android - how to stop repeating alarm from another activity -
i have set repeating alarm on multiple tasks in app. getting task notification properly. when clicking on dismiss button alarm not cancelling. have created alert dialog notification user.
i passing db _id pendingintent
uniqueid
multiple alarms on different tasks. alarm ringing not stopping. please guide me this.
here's activity class setting alarms
protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.alarm_layout); // code.. setalarm.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub idval=idval+1; // fetching id db. alarm_intent= new intent(alarmactivity.this, receiveractivity.class); alarm_intent.putextra("str_text", str_text); alarm_intent.putextra("cls_id", idval); pendingintent = pendingintent.getbroadcast(alarmactivity.this, idval, alarm_intent, pendingintent.flag_update_current); date dat = new date();//initializes calendar cal_alarm = calendar.getinstance(); calendar cal_now = calendar.getinstance(); cal_now.settime(dat); // myear, mmonth, mday, mhours24, mminutes taking vales datepicker & timepicker cal_alarm.set(myear, mmonth, mday, mhours24, mminutes, 0); if(cal_alarm.before(cal_now)) { cal_alarm.add(calendar.date,1); //if in past increment } string temp_val=(string)spinner1.getselecteditem(); alarmmanager noteam = (alarmmanager)getsystemservice(context.alarm_service); if(temp_val.equals("one time")) { noteam.set(alarmmanager.rtc_wakeup, cal_alarm.gettimeinmillis(), pendingintent); } else if(temp_val.equals("every 5 minutes")) { noteam.setrepeating(alarmmanager.rtc_wakeup, cal_alarm.gettimeinmillis(), 300000, pendingintent); } else if(temp_val.equals("every 10 mintues")) { noteam.setrepeating(alarmmanager.rtc_wakeup, cal_alarm.gettimeinmillis(), 600000, pendingintent); } else if(temp_val.equals("every 30 minutes")) { noteam.setrepeating(alarmmanager.rtc_wakeup, cal_alarm.gettimeinmillis(), 1800000, pendingintent); } else if(temp_val.equals("every 1 hour")) { noteam.setrepeating(alarmmanager.rtc_wakeup, cal_alarm.gettimeinmillis(), 3600000, pendingintent); } } });
}
and activity
cancelling alarm
protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); //some code.. clsid=extra.getint("cls_id"); new alertdialog.builder(alertboxnotification.this) .settitle("title") .setmessage("message user") .setpositivebutton(android.r.string.ok, new dialoginterface.onclicklistener() { @suppresslint("newapi") @override public void onclick(dialoginterface arg0, int arg1) { // todo auto-generated method stub finish(); arg0.dismiss(); } }) .setnegativebutton(r.string.dismissbtn, new dialoginterface.onclicklistener() { public void onclick(dialoginterface arg0, int arg1) { intent intent = new intent(alertboxnotification.this, alarmactivity.class); pendingintent psender = pendingintent.getbroadcast(alarmactivity.mycontext = getapplicationcontext(), clsid, intent, 0); alarmmanager = (alarmmanager)getsystemservice(alarm_service); am.cancel(psender); toast.maketext(getapplicationcontext(), "alarm cancel ", toast.length_long).show(); } }).create().show(); }
i have change
pendingintent psender = pendingintent.getbroadcast(alarmactivity.mycontext = getapplicationcontext(), clsid, intent, 0);
to
pisender = pendingintent.getbroadcast(getbasecontext(), clsid, intent, 0);
and made pendingintent
global. now alarm cancelling activity(from dialog box).
Comments
Post a Comment