android - Releasing PendingIntent after reentering app -
anyone knows how cancel pendingintent after leaving app button? when pendingintent switched on , canceled on 1 session without exiting app in between it's fine - got covered, when leave , reenter can't anymore cancel , have wait alarm.
mainactivity:
b5.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { setcall(12000); pendingintent.cancel(); if (alarmmanager!= null) { alarmmanager.cancel(pendingintent); } } }); public void setcall(int timetocall){ if (alarmmanager!= null) { alarmmanager.cancel(pendingintent); } string name = e1.geteditabletext().tostring(); //toast.maketext(getapplicationcontext(), name, toast.length_short).show(); intent intent = new intent(mainactivity.this, thisbroadcastreceiver.class); intent.setflags(intent.flag_include_stopped_packages); intent.putextra("name",name); pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, intent, pendingintent.flag_update_current); alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); alarmmanager.set(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime() + timetocall, pendingintent); }
broadcastreceiver:
@override public void onreceive(context context, intent intent) { string name = intent.getextras().getstring("name"); intent = new intent(context, callscreen.class); i.setclassname("(package)", "(classname)"); i.setflags(intent.flag_activity_new_task); i.putextra("name2", name); context.startactivity(i); toast.maketext(context, name, toast.length_short).show(); }
if want cancel pendingintent after activity destroyed (actually after losing reference original pendingintent object), need recreate object in same way , cancel it.
therefore, create object of pendingintent before trying cancel it. also, call cancel directly on pendingintent object.