java - android notification comes up on start -
i m trying create app, schedule notification whenever try open app, notification comes up..... comes @ right time when app opened. here's code............
main activity.java
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); calendar calendar_object = calendar.getinstance(); calendar_object.set(calendar.hour_of_day,16); calendar_object.set(calendar.minute,18); calendar_object.set(calendar.second,00); intent myintent = new intent(mainactivity.this, alarmreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(mainactivity.this, 0, myintent,0); alarmmanager alarmmanager = (alarmmanager)getsystemservice(context.alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar_object.gettimeinmillis(),120000,pendingintent); } }
alarmreceiver.java
public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent myintent = new intent(context, notificationservice.class); context.startservice(myintent); } }
notificationservice.java
public class notificationservice extends service { private notificationmanager mmanager; @override public ibinder onbind(intent arg0) { return null; } @override public void oncreate() { super.oncreate(); } @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); @suppresswarnings("deprecation") notification notification = new notification(r.drawable.images, "xys", system.currenttimemillis()); intent1.addflags(intent.flag_activity_single_top | intent.flag_activity_clear_top); notification.defaults |= notification.default_sound; notification.defaults |= notification.default_vibrate; pendingintent pendingnotificationintent = pendingintent.getactivity( this.getapplicationcontext(), 0, intent1, pendingintent.flag_one_shot); notification.flags |= notification.flag_auto_cancel; notification.setlatesteventinfo(this.getapplicationcontext(), "xyz", "abcd", pendingnotificationintent); notification.flags|= notification.flag_auto_cancel; mmanager.notify(0, notification); stopself(); }
alarm triggering again because after notification calling mainactivity in setting alarm again. try set alarm in different activity. else prevent alarm setting if activity started after notification.
Comments
Post a Comment