Having some problems creating multiple alarms on android -


i creating todo application user can create list of things remember do. when creating every instance in list set alarm go off when entrys deadline reached. alarms reciever creates notification, showing user deadline reached.

the problem i'm having string i´m passing in intent notification doesn't update properly. first alarm functions well, every entry create following first gets same string passed first.

my code setting alarm follows:

alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service);  intent intent = new intent(context, deadlineactivator.class); intent.setaction("todo" + system.currenttimemillis()); intent.putextra("todoname", name);  pendingintent pendingintent = pendingintent.getbroadcast(context,          (int)system.currenttimemillis(), intent, pendingintent.flag_update_current); am.set(alarmmanager.rtc_wakeup, deadline.gettime(), pendingintent); 

don't know if helpfull in way name of entry unique

the onrecieve method alarm looks this

public void onreceive(context context, intent intent) {     string todoname = intent.getstringextra(todoitem.todoname);      notificationmanager nm = (notificationmanager)context.getsystemservice(context.notification_service);      intent onclickintent = new intent(context, watchtodo.class);     onclickintent.putextra(auth.target_todo, todoname);     pendingintent pintent = pendingintent.getactivity(context, 0, onclickintent, 0);     string body = "deadline of " + todoname + " has been reached";     string title = "deadline reached";     notification n = new notification.builder(context)      .setcontenttitle(title)      .setcontenttext(body)      .setsmallicon(r.drawable.ic_launcher)      .setcontentintent(pintent)      .setautocancel(true)      .build();     nm.notify(notification_id, n); } 

when debugging, see name var use when creating intent alarm has correct value. todoname var in onrecieve keeps getting old value.

this common problem. need understand how pendingintent works. problem when call:

pendingintent pintent = pendingintent.getactivity(context, 0, onclickintent, 0); 

android returns "token" can used launch onclickintent. android stores intent in internally managed list , assigns token it.

the next time call:

pendingintent pintent = pendingintent.getactivity(context, 0, onclickintent, 0); 

android looks in list of pendingintents see if has 1 matches one. since comparison of intent objects does not include extras, finds previous onclickintent , returns exact same token did first time. pass token notification , when notification starts activity, gets first set of extras.

to fix need make sure every call pendingintent.getactivity() returns unique token , android saving each separate instance of onclickintent correct extras. can set second parameter unique value, example:

int uniquevalue = (int)currenttimemillis(); pendingintent pintent = pendingintent.getactivity(context, uniquevalue, onclickintent, 0); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -