android - Alarm Manager shows notification everytime -
i have made android application displays notification message @ particular time (say 6:15:00 pm) of day. have used calendar along alarmmanager class purpose.
however, when installed app on phone test it, showed me message/notification @ specified time continues show same @ time..i mean notification keeps on coming throughout day in phone though have set time in app.
here code 1) activity class
import java.util.calendar; import android.app.activity; import android.app.alarmmanager; import android.app.pendingintent; import android.content.intent; import android.os.bundle; public class mainactivity extends activity { private pendingintent pendingintent; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); calendar calendar = calendar.getinstance(); calendar.set(calendar.month, 6); calendar.set(calendar.year, 2013); calendar.set(calendar.day_of_month, 13); calendar.set(calendar.hour_of_day, 20); calendar.set(calendar.minute, 48); calendar.set(calendar.second, 0); calendar.set(calendar.am_pm,calendar.pm); intent myintent = new intent(mainactivity.this, myreceiver.class); pendingintent = pendingintent.getbroadcast(mainactivity.this, 0, myintent,0); alarmmanager alarmmanager = (alarmmanager)getsystemservice(alarm_service); alarmmanager.set(alarmmanager.rtc, calendar.gettimeinmillis(), pendingintent); } //end oncreate }
2) receiver
import android.content.broadcastreceiver; import android.content.context; import android.content.intent; public class myreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent service1 = new intent(context, myalarmservice.class); context.startservice(service1); } }
3) service class
import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.app.service; import android.content.intent; import android.os.ibinder; public class myalarmservice extends service { private notificationmanager mmanager; @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } @override public void oncreate() { // todo auto-generated method stub super.oncreate(); } @suppresswarnings("static-access") @override public void onstart(intent intent, int startid) { super.onstart(intent, startid); mmanager = (notificationmanager) this.getapplicationcontext().getsystemservice(this.getapplicationcontext().notification_service); intent intent1 = new intent(this.getapplicationcontext(),mainactivity.class); notification notification = new notification(r.drawable.ic_launcher,"this test message!", system.currenttimemillis()); intent1.addflags(intent.flag_activity_single_top| intent.flag_activity_clear_top); pendingintent pendingnotificationintent = pendingintent.getactivity( this.getapplicationcontext(),0, intent1,pendingintent.flag_update_current); notification.flags |= notification.flag_auto_cancel; notification.setlatesteventinfo(this.getapplicationcontext(), "alarmmanagerdemo", "this test message!", pendingnotificationintent); mmanager.notify(0, notification); } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); }
}
i have added both service , receiver in manifest.xml file. please can tell me should display notification once during entire day?
i guess reason oncreate method called multiple times , alarmmanager.set called multiple times. example, whenever screen orientation changed, activity destroyed , new activity starts oncreate() method. every time rotate screen activity destroyed , new activity starts oncreate() method , alarmmanager.set called again.
you may save state member in onsaveinstancestate(bundle bundle) method. android calls method whenever screen rotated, , given bundle bundle passed oncreate(bundle bundle). in oncreate, check bundle null before try call alarmmanager.set.
Comments
Post a Comment