java - Cancel alarm with different context -
i've alarm created in onbootreceiver this:
public class onbootreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { calendar cal = calendar.getinstance(); cal.add(calendar.second, 10); intent = new intent(context, alarmreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, i, pendingintent.flag_cancel_current); alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), savedintervalautomaticmilisint, pendingintent); } }
but cancel it, use code in activity:
intent intent = new intent(this, alarmreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_cancel_current); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.cancel(pendingintent); scmanager.clearerrors();
so set use context broadcastreceiver's context parameter, cancel use this
context activity. question: alarm still cancelled, though context different?
will alarm still cancelled, though context different?
context not matter in case -- merely way alarmmanager
.
your choice of pendingintent.flag_cancel_current
in cancel-the-alarm logic may cause problem. if run difficulties (e.g., adb shell dumpsys alarm
indicates alarm survived cancel), try replacing pendingintent.flag_cancel_current
0
.
Comments
Post a Comment