有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java广播接收器第二次不工作

我正在尝试用AlarmManager来安排通知。当我安排一个通知时,效果非常好,但是当我安排两个通知时,第一个通知可以,但是第二个不起作用

我发现几分钟后打开应用程序会通知第二个通知。我想我的收音机出毛病了

主要活动。java

Intent intent = new Intent(context,NotificationClass.class);
intent.putExtra("notification_id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,id,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

通知。java

public class NotificationClass extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int id = intent.getIntExtra("notification_id",0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"1")
            .setContentTitle("Notification")
            .setContentText("Content")
            .setSmallIcon(R.drawable.notif_ic);

        Notification notification = builder.build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        if (安卓.os.Build.VERSION.SDK_INT >= 安卓.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("1","test", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(id,notification);
}

AndroidManifest。xml

<receiver 安卓:name=".NotificationClass" ></receiver> 

我不知道我的代码出了什么问题。有人能帮我吗


共 (3) 个答案

  1. # 1 楼答案

    接收数据的广播接收器:

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String alertMessage = intent.getStringExtra("type");
    
            doNotificationAlertWorkHere(alertMessage);
        }
    };
    

    登记册及;注销广播以避免静态泄漏

    通过Android清单文件。(静态)

    <receiver android:name="YourBroadcastReceiverName"> </receiver>
    

    通过上下文。registerReceiver()和上下文。unregisterReceiver()方法。(动态)

     @Override
        protected void onPause() {
            super.onPause();
            // unregister broadcast
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            // register broadcast
            IntentFilter filter = new IntentFilter(Constants.ACTION);
            LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
        }
    

    发送广播,如:

    // public static final String ACTION = "ALERT";
    
    Intent intent = new Intent(Constants.ACTION);
    intent.putExtra("type", "SUP BRO. Stay Inside");
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    

    知识提示:-广播接收器就像一门大炮,要获得命中,你必须确定发射什么(如msg),在哪里发射(如活动)。加载和加载;卸下大炮以获得另一次命中。(如注册和注销)

  2. # 2 楼答案

    我已经试过了,它正在起作用。在onReceive中添加通知代码

    广播接收机

    class AlarmReceiver : BroadcastReceiver() {
    
    override fun onReceive(context: Context, intent: Intent) {
    
     /*
      Your implementation
    
       */
     }
    }
    

    主要节日

        <receiver
            android:name=".AlarmReceiver"
            android:exported="true"
            android:enabled="true" />
    

    创建挂起的意图

    val alarmManager = activity.getSystemService(Activity.ALARM_SERVICE) as AlarmManager
            val alarmIntent = Intent(activity.applicationContext, AlarmReceiver::class.java) // AlarmReceiver1 = broadcast receiver
    
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = timeInMilliSeconds
    
            val pendingIntent = PendingIntent.getBroadcast(activity, timeInMilliSeconds.toInt(), alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT)
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)