android - broadcast reciever when app is closed -
i want create alarm clock running if app closed, working fine when app isn't closed, when trigger time , close app, moment time goes off app crashes. tried few solutions didn't help.
here code:
public class mainactivity extends activity { //used register alarm manager pendingintent pendingintent; //used store running alarmmanager instance alarmmanager alarmmanager; //callback function alarmmanager event broadcastreceiver mreceiver; timepicker tp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //register alarmmanager broadcast receive. registeralarmbroadcast(); tp = (timepicker)findviewbyid(r.id.timepicker1); tp.setis24hourview(true); calendar cal=calendar.getinstance(); tp.setcurrenthour(cal.get(calendar.hour_of_day)); tp.setcurrentminute(cal.get(calendar.minute) ); } public void onclicksetalarm(view v) { //get current time , set alarm after 10 seconds current time // here calendar time = calendar.getinstance(); time.set(calendar.hour_of_day, tp.getcurrenthour()); time.set(calendar.minute, tp.getcurrentminute()); time.set(calendar.second, 0); alarmmanager.set( alarmmanager.rtc_wakeup, time.gettimeinmillis(), pendingintent); tp.setenabled(false); // alarmmanager.set( alarmmanager.rtc_wakeup, // system.currenttimemillis() + integer.parseint(time.gettext().tostring() ) * 1000 , pendingintent ); } private void registeralarmbroadcast() { // log.i("alarm example:registeralarmbroadcast()", "going register intent.registeralrambroadcast"); //this phone call function(broadcastreceiver) phone call when //alarm time reached. mreceiver = new broadcastreceiver() { private static final string tag = "alarm illustration receiver"; @override public void onreceive(context context, intent intent) { //log.i(tag,"broadcastreceiver::onreceive() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); toast.maketext(context, "congrats!. alarm time has been reached", toast.length_long).show(); tp.setenabled(true); // define sound uri, sound played when there's notification uri sounduri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); // intent triggered, can add together other intent other actions pendingintent pintent = pendingintent.getactivity(mainactivity.this, 0, new intent("my_alarm_notification").setflags(intent.flag_activity_new_task), 0); // it, we'll build notification! // in addaction method, if don't want icon, set first param 0 notification mnotification = new notification.builder(mainactivity.this) .setcontenttitle("new post!") .setcontenttext("here's awesome update you!") .setsmallicon(r.drawable.ic_launcher) .setcontentintent(pintent) .setsound(sounduri) // .addaction(r.drawable.ninja, "view", pintent) .addaction(0, "remind", pintent) .build(); notificationmanager notificationmanager = (notificationmanager) getsystemservice(notification_service); // if want hide notification after selected, code below // mynotification.flags |= notification.flag_auto_cancel; notificationmanager.notify(0, mnotification); } }; // register alarm broadcast here registerreceiver(mreceiver, new intentfilter("my_alarm_notification") ); pendingintent = pendingintent.getbroadcast( this, 123456789, new intent("my_alarm_notification").setflags(intent.flag_activity_new_task),0 ); alarmmanager = (alarmmanager)(this.getsystemservice( context.alarm_service )); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } } private void unregisteralarmbroadcast() { alarmmanager.cancel(pendingintent); getbasecontext().unregisterreceiver(mreceiver); } @override protected void ondestroy() { unregisterreceiver(mreceiver); super.ondestroy(); } }
any kind of help appreciated.
and here manifest.xml:
<receiver android:name=".mainactivity" > <intent-filter> <action android:name="my_alarm_notification" /> </intent-filter> </receiver>
you've defined activity
<receiver>
in manifest. can't work.
you've got several problems here.
yourbroadcastreceiver
must declared in manifest <receiver>
tag. means class using broadcastreceiver
must public. in code, you've declared broadcastreceiver
anonymous class. in general work, cannot work if broadcastreceiver
needs called components outside app. alarmmnager
runs outside of app (indeed trigger alarms when app not running), hence broadcastreceiver
must defined public class extends broadcastreceiver
. you calling pendingintent.getactivity()
. if want start broadcastreceiver
need phone call pendingintent.getbroadcast()
instead. in onreceive()
method, making utilize of fellow member variables null
in case alarm triggered while app not running. since alarmmanager
can trigger alarm if app not running, cannot expect any fellow member variables have been set when onreceive()
called. i'd suggest @ examples of how this. there tons of them available.
android android-activity android-broadcast android-alarms
No comments:
Post a Comment