android - snooze not working for alarm manager -
snoozebtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent myintent = new intent(getapplicationcontext(), alarmalertbroadcastreciever.class); myintent.putextra("alarm", this); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, myintent,pendingintent.flag_cancel_current); alarmmanager alarmmanager = (alarmmanager)getapplicationcontext().getsystemservice(context.alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, getalarmtime().gettimeinmillis(), pendingintent); toast.maketext(getapplicationcontext(), "snoozed 10 mins", toast.length_long); finish(); } });
when click snooze button, alarm must restart after 10 mins , not working.
i set alarm after 10 seconds, logcat after 10sec when click snooze button
full logcat
enter code here
public class alarmalertactivity extends activity implements onclicklistener { private calendar alarmtime = calendar.getinstance(); private alarm alarm; private mediaplayer mediaplayer; private stringbuilder answerbuilder = new stringbuilder(); private vibrator vibrator; private boolean alarmactive; private textview problemview; private textview answerview; private textview msg_txt, alarmname_txt; private imageview img1; spinner spinner_snooze; private string answerstring; int hr1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); final window window = getwindow(); window.addflags(windowmanager.layoutparams.flag_show_when_locked | windowmanager.layoutparams.flag_dismiss_keyguard); window.addflags(windowmanager.layoutparams.flag_keep_screen_on | windowmanager.layoutparams.flag_turn_screen_on); setcontentview(r.layout.simple); bundle bundle = this.getintent().getextras(); alarm = (alarm) bundle.getserializable("alarm"); button can1 = (button) findviewbyid(r.id.can1); button snoozebtn = (button) findviewbyid(r.id.snoozebtn); animation shake = animationutils.loadanimation(getapplicationcontext(), r.anim.shake); animation shakeimage = animationutils.loadanimation(getapplicationcontext(), r.anim.shakeimage); msg_txt = (textview) findviewbyid(r.id.msg_txt); alarmname_txt = (textview) findviewbyid(r.id.alarmname_txt); img1 = (imageview) findviewbyid(r.id.img1); img1.setimageresource(r.drawable.smileyblack); calendar c = calendar.getinstance(); // current time hr1 = c.get(calendar.hour_of_day); // gets current hour of day ( 1 24 ) startalarm(); if (hr1 < 12) { findviewbyid(r.id.img1).startanimation(shakeimage); msg_txt.settext("good morning!"); alarmname_txt.settext(alarm.getalarmname()); } else if(hr1 > 12 && hr1 < 17) { findviewbyid(r.id.msg_txt).startanimation(shake); findviewbyid(r.id.img1).startanimation(shakeimage); msg_txt.settext("good afternoon!"); alarmname_txt.settext(alarm.getalarmname()); } else if(hr1 > 17 && hr1 < 20) { findviewbyid(r.id.img1).startanimation(shakeimage); msg_txt.settext("good evening!"); alarmname_txt.settext(alarm.getalarmname()); } else { findviewbyid(r.id.msg_txt).startanimation(shake); msg_txt.settext("good night!"); alarmname_txt.settext(alarm.getalarmname()); } can1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { finish(); } }); snoozebtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { calendar c1 = calendar.getinstance(); // current time long gettimeaftertenminute = c1.gettimeinmillis() + 5000; intent myintent = new intent(getapplicationcontext(), alarmalertbroadcastreciever.class); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 1, myintent,pendingintent.flag_cancel_current); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, gettimeaftertenminute,pendingintent ); toast.maketext(getapplicationcontext(), "snoozed 30 sec ", toast.length_long); finish(); } }); } @override protected void onresume() { super.onresume(); alarmactive = true; } private void startalarm() { if (alarm.getalarmtonepath() != "") { mediaplayer = new mediaplayer(); if (alarm.getvibrate()) { vibrator = (vibrator) getsystemservice(vibrator_service); long[] pattern = { 1000, 200, 200, 200 }; vibrator.vibrate(pattern, 0); } try { mediaplayer.setvolume(1.0f, 1.0f); mediaplayer.setdatasource(this, uri.parse(alarm.getalarmtonepath())); mediaplayer.setaudiostreamtype(audiomanager.stream_alarm); mediaplayer.setlooping(true); mediaplayer.prepare(); mediaplayer.start(); } catch (exception e) { mediaplayer.release(); alarmactive = false; } } } @override public void onbackpressed() { if (!alarmactive) super.onbackpressed(); } @override protected void ondestroy() { try { if (vibrator != null) vibrator.cancel(); } catch (exception e) { } try { mediaplayer.stop(); } catch (exception e) { } try { mediaplayer.release(); } catch (exception e) { } super.ondestroy(); }
} }
this alarm activity
it easy snooze alarm set the alarm same id not need cancel previous alarm following example.
pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), sameid, myintent,pendingintent.flag_cancel_current); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, gettimeaftertenminute,pendingintent );
please edit sameid variable in sample code same number given @ time of setting alarm.this cancel alarm , set new one.
as logcat suggest there must object trying access null inside oncreate method of alarmalertactivity.
please paste alarmalertactivity oncreate method.
hope :).