java - I'm Making an android Alarm Application but when I close the app the alarm is cancelled. Here's my code -


public class alarmservice extends intentservice { private notificationmanager alarmnotificationmanager;

public alarmservice() {     super("alarmservice"); }  @override public void onhandleintent(intent intent) {     sendnotification("wake up! wake up!"); }  private void sendnotification(string msg) {     log.d("alarmservice", "preparing send notification...: " + msg);     alarmnotificationmanager = (notificationmanager)             .getsystemservice(context.notification_service);      pendingintent contentintent = pendingintent.getactivity(this, 0,             new intent(this, alarmactivity.class), 0);      notificationcompat.builder alamnotificationbuilder = new notificationcompat.builder(             this).setcontenttitle("alarm").setsmallicon(r.drawable.ic_launcher)             .setstyle(new notificationcompat.bigtextstyle().bigtext(msg))             .setcontenttext(msg);       alamnotificationbuilder.setcontentintent(contentintent);     alarmnotificationmanager.notify(1, alamnotificationbuilder.build());     log.d("alarmservice", "notification sent."); } 

}

public class alarmactivity extends activity {

alarmmanager alarmmanager; private pendingintent pendingintent; private timepicker alarmtimepicker; private static alarmactivity inst; private textview alarmtextview;  public static alarmactivity instance() {     return inst; }  @override public void onstart() {     super.onstart();     inst = this; }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_alarm);     alarmtimepicker = (timepicker) findviewbyid(r.id.alarmtimepicker);     alarmtextview = (textview) findviewbyid(r.id.alarmtext);     togglebutton alarmtoggle = (togglebutton) findviewbyid(r.id.alarmtoggle);     alarmmanager = (alarmmanager) getsystemservice(alarm_service);     mediaplayer mp;     mp=mediaplayer.create(alarmactivity.this, r.raw.alarm);     mp.start(); }  public void ontoggleclicked(view view) {     if (((togglebutton) view).ischecked()) {         log.d("myactivity", "alarm on");         calendar calendar = calendar.getinstance();         calendar.set(calendar.hour_of_day, alarmtimepicker.getcurrenthour());         calendar.set(calendar.minute, alarmtimepicker.getcurrentminute());         intent myintent = new intent(alarmactivity.this, alarmreceiver.class);         pendingintent = pendingintent.getbroadcast(alarmactivity.this, 0, myintent, 0);         alarmmanager.set(alarmmanager.rtc, calendar.gettimeinmillis(), pendingintent);     } else {         alarmmanager.cancel(pendingintent);         setalarmtext("alarm canceled");         log.d("myactivity", "alarm off");       } }  public void setalarmtext(string alarmtext) {     alarmtextview.settext(alarmtext); } 

}

public class alarmreceiver extends wakefulbroadcastreceiver {

@override public void onreceive(final context context, intent intent) {     //this update ui message     alarmactivity inst = alarmactivity.instance();     inst.setalarmtext("it's time wake :)");      //this sound alarm tone     //this sound alarm once, if wish     //raise alarm in loop continuously use mediaplayer , setlooping(true)     uri alarmuri = ringtonemanager.getdefaulturi(ringtonemanager.type_alarm);     if (alarmuri == null) {         alarmuri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);     }     ringtone ringtone = ringtonemanager.getringtone(context, alarmuri);     ringtone.play();      //this send notification message     componentname comp = new componentname(context.getpackagename(),             alarmservice.class.getname());     startwakefulservice(context, (intent.setcomponent(comp)));     setresultcode(activity.result_ok); } 

}


Comments