如何更新广播接收器。 (多次通知)

时间:2017-05-04 16:33:25

标签: android broadcastreceiver android-broadcastreceiver notificationmanager

每次设置通知/警报管理器时,BroadcastReceiver中的变量都不会更新。

enter image description here

enter image description here

“receiver(recycler)”来自片段。

receiver“来自BroadcastReceiver类。

onCreateView

intentAlarmManager = new Intent(context, NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarmManager, PendingIntent.FLAG_UPDATE_CURRENT);

通知方法

    private void setNotification(int hour, int min, int interval, int uniqueID) {

    //get instance of the calendar
    calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);
    //create delayed intent
    pendingIntent = PendingIntent.getBroadcast(context, uniqueID, intentAlarmManager, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * (interval * 30), pendingIntent);


}

它连接到交换机监听器的Recycler

setNotification(Integer.parseInt(model.getHour()), Integer.parseInt(model.getMinute()), Integer.parseInt(model.getInterval()), Integer.parseInt(model.getTime()));

接收机

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentToStartWhenAlarmSets = new Intent(context, LoginActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intentToStartWhenAlarmSets, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Content Title")
                .setContentText("Notify " + HomeFragment.notifMedName)
                .setSound(notifSound)
                .setVibrate(pattern)
                //swipable
                .setAutoCancel(true);
        Log.d(ContentValues.TAG, "receiver " + HomeFragment.notifMedName);

        notificationManager.notify((int) System.currentTimeMillis(), builder.build());

1 个答案:

答案 0 :(得分:0)

我想,我想出来了。虽然我不确定这是否是最佳/正确的方式(虽然我正在工作)。

所以在我的接收器课程中我添加了

intent.getStringExtra("string);

在我的片段中我添加了

intentAlarmManager.putExtra("string", notifMedName);
getActivity().sendBroadcast(intentAlarmManager);

并且我还将所有待处理的意图从context更改为getActivity()

如果你们有更好的解决方案,请随时回答。

More info here.