Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - PendingIntent.FLAG_CANCEL_CURRENT - does it really cancel alarmManager previous pending intent?

I have the following code which simply runs an alarm manager:

 public void runAlarm(){
   Intent intent = new Intent(context, MyReceiver.class);
          intent.setAction(ACTION_TIMEOUT);
          PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

          setTimeOutAlarm(TIMEOUT_MINUTES,alarmIntent);
          AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
                      alarmMgr.setExact(AlarmManager.RTC,
                              Calendar.getInstance().getTimeInMillis() + 3*60*1000, alarmIntent);

}

It works fine. My question is about the pending intent flag: PendingIntent.FLAG_CANCEL_CURRENT

if i run this method twice does it really cancel out the other pending intent thats already in the alarmManager? how does it know its not to cancel it ?I just want to make sure im not causing any leaks by using this flag. My intent is that i can run this code multiple times and it will keep canceling the previous pendingIntent already sent to the alarmManager instance.

But everytime i run this code the alarm count grows by one when i check with this adb command:

adb shell dumpsys alarm | grep com.your.package

so it seems the alarmManager is not canceled perhaps.

like image 295
j2emanue Avatar asked Oct 19 '22 14:10

j2emanue


1 Answers

I believe you don't need to cancel the old alarm.. but just update it.

I think you can use: PendingIntent.FLAG_UPDATE_CURRENT

Please, make some tests and let me know if it works.

like image 136
W0rmH0le Avatar answered Oct 29 '22 19:10

W0rmH0le