I am updating the data in the cycle from the Internet (so far in the cycle, then move on to a more acceptable option). Every time after the update, I get a list of notifications that I need to display at a certain time (the object with the notification is called Notification , not to be confused with Android notifications). To display notifications - I run the AlarmManager at intervals of half a second (because Android sometimes makes inaccuracies, then I will correct it for a more reasonable option), so that it checks whether it is necessary to display notifications, and in Intent I pass it a serialized array of notifications.

Before starting the installation of new notifications, I delete the old ones. The code looks like this:

 NotificationShower.stopNotifications(mContext); NotificationShower.startNotifications(mContext, notifications); 

The class that is responsible for notifications looks like this (only the code that matters):

 public class NotificationShower extends BroadcastReceiver { public static final String TAG = "RLOG"; public static final String EXTRA_NOTIFICATIONS_ARRAY = "notifications_array"; ... @Override public void onReceive(Context context, Intent intent) { String currentTime = dataFormat.format(System.currentTimeMillis()); Log.i(TAG, "time = " + currentTime); Notification[] notifications = (Notification[]) intent.getExtras().getSerializable(EXTRA_NOTIFICATIONS_ARRAY); if (notifications == null) { Log.i(TAG, "notifications is null"); return; } ... } ... ... ... public static void startNotifications(Context context, Notification[] notifications) { Intent intent = new Intent(context, NotificationShower.class); intent.putExtra(EXTRA_NOTIFICATIONS_ARRAY, notifications); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10 * 1000, 30 * 1000, pendingIntent); } public static void stopNotifications(Context context) { Intent intent = new Intent(context, NotificationShower.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); } } 

The problem is that if I call the stopNotifications method before startNotifications , an empty Intent stopNotifications to the startNotifications method, which does not contain the array I need! No attempt to stop - it works fine.


I can not understand how this methods are so connected that .cancel causes an empty .cancel to be transmitted? What is the problem, why, after a stop and a new launch, an empty Intent transmitted.

  • 2
    And if in cancel instead of FLAG_UPDATE_CURRENT to transfer FLAG_CANCEL_CURRENT? - Andrew Grow
  • one
    Yes, there must be a Cancel. Here are proven methods for starting / canceling an alarm: ru.stackoverflow.com/a/534643/17609 - JuriySPb
  • @Yuriy SPb, at random, I learned that the error was in the absence of the pendingIntent.cancel(); ... - user189127 5:43
  • Write this in response) - Yuriy SPb
  • @ YuriySPb, I found it with you, so you write :)). - user189127

1 answer 1

Adding cancellation to PendingIntent should also help PendingIntent

 pendingIntent.cancel(); 

Also, do not be FLAG_UPDATE_CURRENT to replace FLAG_UPDATE_CURRENT with FLAG_CANCEL_CURRENT