Android getExtras/Intent different than expected -


in activity managing list of user supplied food, , setting alarms based on when expire. reason intent passed onrecieve not 1 thought should be.

for example user enters berries expiration date 5/8 , butter expiration 6/4 , hits save data. each item method called. each time call method, save expiration date using putextra intent.

    public void setonetimealarm(int daysafterset) {     //declare intent using class handle alarm     intent intent = new intent(this, foodexpalarm.class);     //retrieve pending intent broadcast, flag 1 shot means set once     intent.putextra("expdate", expdate);     pendingintent pendingintent = pendingintent.getbroadcast(this, 0,       intent, pendingintent.flag_one_shot);     //params: specify use system clock use rtc_wakeup wakeup phone notification,     //time wait, intent     alarmman.set(alarmmanager.rtc_wakeup,       system.currenttimemillis() + (daysafterset * alarmmanager.interval_day), pendingintent);  } 

this causes 1 alarm set berries because close expiration , no alarm butter (which month away expiration). foodexpalarm class called once , notification berries correctly displayed. however, problem comes when next batch of food entered. next user enters bacon expiring 5/8 , bananas 5/15. setonetimealarm called twice again (once each) , foodexpalarm called again once bacon.

however, in foodexpalarm extras passed in butter , not bacon. makes no sense me because foodexpalarm should called after time has elapsed, , should called pending intent corresponding time scheduled. however, not seem case seems intents (or @ least extras) correspond order in foods added.

execution summary:

  • add berries 5/8
  • add butter 6/4
  • save
  • correctly display berry notification
  • add bacon 5/8
  • add bananas 5/15
  • save
  • incorrectly display butter

my question is, why getting intent/extras butter , not bacon? understanding of intents wrong? how can fix this?

foodexpalarm:

    public void onreceive(context context, intent intent) {      bundle exp = intent.getextras(); //this gets extras butter, not bacon want     object temp = exp.get("expdate");       enter_foods.expdate = new datetime(temp);     intent.getextras();     int id = (int) (enter_foods.expdate).getmillis();     ....      notificationman = (notificationmanager) context             .getsystemservice(context.notification_service);     //i think gets pending intent alarm called method     pendingintent contentintent = pendingintent.getactivity(context, 0,         new intent(context,mainactivity.class), intent.flag_activity_new_task);     //creates notification object/icon, , set text flow across top bar     notification notif = new notification(r.drawable.ic_launcher,         "food expiring soon", system.currenttimemillis());     //specify display when notification shown     notif.setlatesteventinfo(context, from, message, contentintent);     //use nofication manager send message phone, update if same id     notificationman.notify(id, notif);     } 

i think getting conflicts due problems alarm manager , intents. intents appear same intent, relative intent comparing method used alarm manager, differ in "extras" alarm's killing each other.

here quote the documentation alarmmanager.set method:

schedule alarm. note: timing operations (ticks, timeouts, etc) easier , more efficient use handler. if there alarm scheduled same intentsender, first canceled.

if time occurs in past, alarm triggered immediately. if there alarm intent scheduled (with equality of 2 intents being defined filterequals(intent)), removed , replaced one.

and in filterequals(intent) documentation:

determine if 2 intents same purposes of intent resolution (filtering). is, if action, data, type, class, , categories same. not compare data included in intents.


Comments