Android: Notifications are sent whenever phone reboots -


i want display notification every 3 hours 10am 22pm, application displaying notification whenever users open phone. in order solve every time notification process triggered set current time plus 3 hours in shared preference. use time value trigger repeating alarm not seems work correctly.

how can display notification @ scheduled time of repeating alarm(every 3 hours) , stop sending notification whenever reboot phone?

my code following:

a broadcast receiver trggered wheneven users reboot phone calling following alarmservice :

public class alarmservice extends service {       private static final string tag = "myservice";      @override     public void onstart(intent intent, int startid) {          calendar calendar  ;              int sp= sharedprefs.getdefaults("time", this);      intent = new intent(this, notificationbaralarm.class);     i.addflags(intent.flag_activity_new_task);      pendingintent pi = pendingintent.getbroadcast(this, 0, i,                   pendingintent.flag_update_current);       calendar = calendar.getinstance();       calendar.set(calendar.hour_of_day, sp);      calendar.set(calendar.minute, 00);       calendar.set(calendar.second, 00);            alarmmanager = (alarmmanager) getsystemservice(context.alarm_service);          am.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(),3*60*60*1000 , pi);      }      @override     public ibinder onbind(intent intent) {     return null;     }      @override     public void ondestroy() {     toast.maketext(this, "my service stopped", toast.length_long).show();     log.d("tag", "ondestroy");     }  }    

which sending intent following broadcast receiver:

public class notificationbaralarm extends broadcastreceiver {      notificationmanager notifymanager;      @override     public void onreceive(context context, intent intent) {          log.d("notificationalarm", "onreceive");           time time = new time();         long currenttimemilliseconds = system.currenttimemillis();         time.set(currenttimemilliseconds);         int t = time.hour;           long updatedtime= currenttimemilliseconds + 10800000;          time nextalarmmill = new time();         nextalarmmill.set(updatedtime);         int nextalarm = nextalarmmill.hour;           sharedprefs.setdefaults("time", nextalarm, context);           log.d("update time", "the next alarm set at"+" "+ nextalarm);           notifymanager = (notificationmanager) context                 .getsystemservice(context.notification_service);          if (t >= 10 && t <= 22) {              // activity started when user clicks notification             // in notification bar             intent notificationintent = new intent(context,                     alarmreceiveractivity.class);             pendingintent contentintent = pendingintent.getactivity(context, 0,                     notificationintent, 0);             notification notif = new notification(r.drawable.ic_launcher,                     "a new notification popped in!",                     system.currenttimemillis());              // sound when notification shows up.             uri alarmsound = ringtonemanager                     .getdefaulturi(ringtonemanager.type_notification);             notif.sound = alarmsound;             notif.setlatesteventinfo(context, "questionnaire",                     "please fill up", contentintent);              notifymanager.notify(1, notif);          }       }  }   here code of shared preferences:  public class sharedprefs {       public static void setdefaults(string key, int value, context context) {          sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(context);         sharedpreferences.editor editor = prefs.edit();         editor.putint(key, value);         editor.commit();      }      public static int getdefaults(string key, context context) {          sharedpreferences preferences = preferencemanager.getdefaultsharedpreferences(context);         return preferences.getint(key, 10);      } 

in code asking alarm go off immedialtely first time when alarm set.

try am.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis() + 3*60*60*1000, 3*60*60*1000 , pi);


Comments