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?