android - Use notifications and alarm -


i use notifications , alarm in android app not understand why if set alarm, see notification every time launch app. mainactivity:

public void setrepeatingalarm() {          intent intent = new intent(this, myalarmservice.class);            pendingintent pendingintent = pendingintent.getbroadcast(this, 0,          intent, pendingintent.flag_cancel_current);           long startat;          long period;           sharedpreferences mpref = context.getsharedpreferences("pref_name", context.mode_private);           long dif = system.currenttimemillis() - mpref.getlong("update_time", 0);           if (dif >= update_period) {            startat = 0;            period = update_period;          } else {            startat = dif;            period = dif;          }            am.setrepeating(alarmmanager.rtc_wakeup, startat, period,pendingintent); } 

this myalarmservice:

public class myalarmservice extends broadcastreceiver {   notificationmanager nm;   @override  public void onreceive(context context, intent intent) {     nm = (notificationmanager) context     .getsystemservice(context.notification_service);   charsequence = "locali torino";   charsequence message = "visita le serate!";   intent action = new intent(context, mainactivity.class);      pendingintent contentintent = pendingintent.getactivity(context, 0,     action, 0);      notification notif = new notification(r.drawable.disco,     "visita le serate!", system.currenttimemillis());   notif.setlatesteventinfo(context, from, message, contentintent);   notif.flags |= notification.flag_auto_cancel;   nm.notify(0, notif);     sharedpreferences mpref = context.getsharedpreferences("pref_name", context.mode_private);     sharedpreferences.editor meditor = mpref.edit();        long time = system.currenttimemillis();     meditor.putlong("update_time", time);     meditor.commit();  } } 

i see notification every day @ same time , not every time open app. thank you.

edit

this oncreate:

 @override public void oncreate(bundle savedinstancestate) {    super.oncreate(savedinstancestate);    setcontentview(r.layout.activity_main);    strictmode.enabledefaults(); //strict mode enabled     context=mainactivity.this;      = (alarmmanager) getsystemservice(context.alarm_service);    setrepeatingalarm();         locale = (textview) findviewbyid(r.id.locale7);    lunedi = (textview) findviewbyid(r.id.lunedi7);    martedi = (textview) findviewbyid(r.id.martedi7);    mercoledi = (textview) findviewbyid(r.id.mercoledi7);    giovedi = (textview) findviewbyid(r.id.giovedi7);    venerdi = (textview) findviewbyid(r.id.venerdi7);    sabato = (textview) findviewbyid(r.id.sabato7);    domenica = (textview) findviewbyid(r.id.domenica7);     typeface font = typeface.createfromasset(getassets(), "chunkfive.otf");    locale.settypeface(font);    lunedi.settypeface(font);    martedi.settypeface(font);    mercoledi.settypeface(font);    giovedi.settypeface(font);    venerdi.settypeface(font);    sabato.settypeface(font);    domenica.settypeface(font);      getdata();    } 

first thing everytime activity called update_period 0 .so startup zero.

second - everytime launch application calling setrepeatingalarm() oncreate of activity. alarm getting rest evertime.

third -add current time setrepeating() otherwise alarm called moment set.

so make other change(as per your requirement) , 1 too.

 am.setrepeating(alarmmanager.rtc_wakeup,system.currenttimemillis()+startat, period,pendingintent); 

update :

store update_period period in sharedpreference , retrieve before doing thing in oncreate


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -