Android alarmmanager cancel and start not working -
i have following code set alarm when main activity starts , cancel when logging out
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); ... // schedule alarm intent intent = new intent(this, myreceiver.class); alarmintent = pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_update_current); alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); if (pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_no_create) == null) { alarmmanager.setinexactrepeating(alarmmanager.elapsed_realtime, 0, alarmmanager.interval_day, alarmintent); } } private void logout() { if (alarmmanager != null) { alarmintent.cancel(); alarmmanager.cancel(alarmintent); } intent = new intent(mainactivity.this, loginactivity.class); i.addflags(intent.flag_activity_clear_task); startactivity(i); finish(); }
but when log out, log in, , enter main activity code alarmmanager.setinexactrepeating()
never reached.
that line never reached, because pendingintent
checking exist, because creating pendingintent
2 lines earlier. since pendingintent
exists, flag_no_create
call still return it, , not equal null
.