java - Notify loop with up toolbar button android -


first of sorry horrible english, problem:

i have mainactivity.class set alarmmanager, @ specific time alarmreceiver check on database if there specific record. if check result false, start service show notification.

the notification open mealactivity.class give user's possibilities insert record on database (the same of alarmreceiver's check) , set new alarm (at least +5 hours).

the problem begin when, on mealactivity.class, without insert record, click on "up button" on toolbar on father activity (mainactivity.class) , here, in mainactivity, receive new notification, same previous. why should not because next notification alarm has been set @ least 5 hours later, more few seconds.

why?

in mealactivity, if press "up button", want come mainactivity without notification. next notification should appear @ appointed time.

how can resolve this?

mainactivity.class:

public class mainactivity extends appcompatactivity {  private alertdialog alertdialog;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      /**      * set alarm food      */     calendar calendar = new gregoriancalendar();      int mealtype;     if((calendar.get(calendar.hour_of_day) >= 0 && calendar.get(calendar.hour_of_day) < 11) || (calendar.get(calendar.hour_of_day) >= 23 && calendar.get(calendar.hour_of_day) < 24)){         mealtype = 0;     }     else if(calendar.get(calendar.hour_of_day) >= 11 && calendar.get(calendar.hour_of_day) < 16){         mealtype = 1;     }     else mealtype = 2;      setalarm(mealtype);  }  public void setalarm(int mealtype){      /**      * set standard alarm.      */     alarmmanager = (alarmmanager) getsystemservice(context.alarm_service);     intent = new intent(mainactivity.this, alarmreceiver.class);     i.putextra("mealtype", mealtype);     pendingintent pi = pendingintent.getbroadcast(mainactivity.this, 0, i, pendingintent.flag_one_shot);      calendar c = new gregoriancalendar();     if(mealtype == 0 && c.get(calendar.hour_of_day) >= 23) {         c.add(calendar.day_of_month, 1);         c.set(calendar.hour_of_day, 11);     }     else if(mealtype == 0 && c.get(calendar.hour_of_day) < 11) {         c.set(calendar.hour_of_day, 11);     }     else if(mealtype == 1){         c.set(calendar.hour_of_day, 16);     }     else c.set(calendar.hour_of_day, 23);      c.set(calendar.minute, 0);     c.set(calendar.second, 0);      am.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), pi);      } // other stuff..  } 

my alarmreceiver:

public class alarmreceiver extends broadcastreceiver {   @override public void onreceive(context context, intent intent) {       log.d("alarm receiver", "allarm received");      int mealtype = intent.getextras().getint("mealtype");      databaseaccess databaseaccess = databaseaccess.getinstance(context);     databaseaccess.open();     int userid = databaseaccess.getactualid();     boolean thereisameal = databaseaccess.thereisameal(userid);     databaseaccess.close();      intent service1 = new intent(context, alarmservice.class);      if(!thereisameal) {          log.d("alarm receiver", "start service");          service1.putextra("mealtype", mealtype);         context.startservice(service1);     }     } } 

my alarmservice:

public class alarmservice extends intentservice {  private static final int notification_id = 1; private static final string tag = "mealalarm"; private notificationmanager notificationmanager; private pendingintent pendingintent;   public alarmservice() {     super("alarmservice"); }   @override public int onstartcommand(intent intent, int flags, int startid) {     return super.onstartcommand(intent,flags,startid); }  @override protected void onhandleintent(intent intent) {      log.d(tag, "alarm service has started.");       context context = this.getapplicationcontext();     notificationmanager = (notificationmanager)context.getsystemservice(context.notification_service);     int mealtype = intent.getextras().getint("mealtype");      intent mintent = new intent(this, mealactivity.class);     mintent.putextra("origin", "notify");     mintent.putextra("mealtype", mealtype);      pendingintent = pendingintent.getactivity(context, 0, mintent, pendingintent.flag_one_shot);      resources res = this.getresources();     notificationcompat.builder builder = new notificationcompat.builder(this);      builder.setcontentintent(pendingintent)             .setsmallicon(r.drawable.icon)             .setlargeicon(bitmapfactory.decoderesource(res, r.drawable.icon))             .setticker(res.getstring(r.string.notification_title))             .setautocancel(true)             .setcontenttitle(res.getstring(r.string.notification_title))             .setcontenttext(res.getstring(r.string.notification_subject_food))             .setvibrate(new long[]{1000, 1000, 1000, 1000, 1000})             .setsound(ringtonemanager.getdefaulturi(ringtonemanager.type_notification));      builder.getnotification().flags |= notification.flag_auto_cancel;      notificationmanager = (notificationmanager) getsystemservice(notification_service);     notificationmanager.notify(notification_id, builder.build());     log.i(tag, "notifications sent.");      stopself();      } } 

mealactivity:

public class mealactivity extends appcompatactivity {   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);       try {         intent intent = getintent();         if (intent.getextras().getstring("origin").compareto("notify") == 0) {              int mealtype = intent.getextras().getint("mealtype");              databaseaccess databaseaccess = databaseaccess.getinstance(this);             databaseaccess.open();             int actualid = databaseaccess.getactualid();             databaseaccess.close();              alarmmanager = (alarmmanager) getsystemservice(context.alarm_service);             intent = new intent(mealactivity.this, alarmreceiver.class);              mealtype = nextmeal(mealtype);              i.putextra("mealtype", mealtype);             pendingintent pi = pendingintent.getbroadcast(mealactivity.this, 0, i, pendingintent.flag_one_shot);              calendar c = calendar.getinstance();             if(mealtype == 0 && c.get(calendar.hour_of_day) >= 23) {                 c.add(calendar.day_of_month, 1);                 c.set(calendar.hour_of_day, 11);             }             else if(mealtype == 0 && c.get(calendar.hour_of_day) < 11) {                 c.set(calendar.hour_of_day, 11);             }             else if(mealtype == 1){                 c.set(calendar.hour_of_day, 16);             }             else c.set(calendar.hour_of_day, 23);              c.set(calendar.minute, 0);             c.set(calendar.second, 0);              am.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), pi);          }     } catch (nullpointerexception ex){          } // other stuff... } } 

thanks help.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -