Skip to content Skip to sidebar Skip to footer

Unable To Set Proper Context To Cancel Alarmmanager

I have created an alarm with a notification and a button to stop the alarm but it seems that there is some issue with the Contextthat I have to get in order to cancel the AlarmMana

Solution 1:

I am using 0 instead PendingIntent.FLAG_UPDATE_CURRENT and cancel method work fine, Try to use 0 in both intents or try to set PendingIntent.FLAG_CANCEL_CURRENT to the pending intent in your private void cancelAlarm(Context context) method.

Solution 2:

All your mucking around with Context is causing your problem. You absolutely positively cannot do this:

privatestaticContextctx= ContextBitchApp.getContext();
privatestaticfinalContextctxCopy= ctx;

a static variable gets initialized when the class is loaded. At this time, you have no idea what ContextBitchApp.getContext() will return.

In any case, you don't need to try to save Context and all that. Just always use getApplicationContext() when you need a Context to pass as a parameter to PendingIntent.getBroadcast().

Also, after calling AlarmManager.cancel(), you can also cancel the PendingIntent by calling cancel() on it.

Post a Comment for "Unable To Set Proper Context To Cancel Alarmmanager"