-1

I was implementing an AlarmManager with an Intent which tryes to connect to a server each 30 minutes until the server has return 200.

if (resultCode != 200){
    alarmMgr = (AlarmManager) ApplicationContextProvider.getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(ApplicationContextProvider.getContext(), MAUAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(ApplicationContextProvider.getContext(), 0, intent, 0);
    alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_HALF_HOUR, AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);        
}

When the server has returned 200, then, the alarm is stopped using this code:

if (resultCode == 200){
    if (alarmMgr!= null) {
        alarmMgr.cancel(alarmIntent);
    }
}

The problem is that if this alarm is alive for some hours, and then, the server returns 200, when it tryes to cancel the alarm, alarmMgr variable is "null" because it's cleaned by the operating system. Then, the alarm is being called each 30 mins instead of being cancelled. How can I cancel the alarm then in this case?

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

0

Do not cache the AlarmManager object. There are ways to retrieve that same Alarm using ID of alarm, in your case you are using 0 as id of that.

You can use below code to retrieve that same alarm and can cancle it.

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ApplicationContextProvider.getContext(), MAUAlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(ApplicationContextProvider.getContext(), 0, intent, 0);
alarmManager.cancel(pendingIntent);
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • creating a new instance of the alarm will work for cancel the previous alive instance? are you 100% sure? – NullPointerException Jan 19 '18 at 09:05
  • @NullPointerException See updates.. and give me few minutes I am updating it with docs :) – Pankaj Kumar Jan 19 '18 at 09:07
  • also will my application class return a context when the rest of the app is garbage collected after some hours of being closed? I ask you because in your solution you are using my application class to create a new instance with that context "ApplicationContextProvider.getContext()" – NullPointerException Jan 19 '18 at 09:08
  • @NullPointerException Yes until your application not killed by user, it will be available. And if your application is killed by user, your alarm will be canceled at that time. – Pankaj Kumar Jan 19 '18 at 09:12
  • I removed the application from recent applications list, which more or less means to be killed. The alarm is still ON. Will application class return a context in that case? I don't want an exception. – NullPointerException Jan 19 '18 at 09:14
  • I think you should check. I am not sure about this. And if this does not work, you have one alternative. You can place this cancel code inside MAUAlarmReceiver.java, inside it you will have context for sure. So you can have logic to cancel alarm or do the task. – Pankaj Kumar Jan 19 '18 at 09:21
  • how can i extract a context from MAUAlarmReceiver? – NullPointerException Jan 19 '18 at 09:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163479/discussion-between-pankaj-kumar-and-nullpointerexception). – Pankaj Kumar Jan 19 '18 at 10:44