android - Service and Chronometer Synchronization -
i need able start chronometer, close activity, after through notifications, activity, , see right time in chronometer.
what i've done
a part of activity:
public void doclick(view target) { switch(target.getid()) { case r.id.buttonstart: { mchronometer.setbase(systemclock.elapsedrealtime()); mchronometer.start(); intent intent = new intent(recentactivity.this, chronometerservice.class); intent.putextra("task_name",task_name); intent.putextra("task_id",task_id); intent.putextra("ellapsedtime",mchronometer.getbase()); log.d("base",""+mchronometer.getbase()); startservice(intent); break; } case r.id.buttonstop: { stopservice(new intent(recentactivity.this, chronometerservice.class)); mchronometer.stop(); mchronometer.setbase(systemclock.elapsedrealtime()); break; } case r.id.button3: { break; } } }
a part of service:
public class chronometerservice extends service { private threadgroup mythreads = new threadgroup("serviceworker"); private notificationmanager notificationmgr; private int task_id; private long ellapsedtime; @override public void oncreate() { super.oncreate(); notificationmgr = (notificationmanager)getsystemservice(notification_service); } @override public int onstartcommand(intent intent, int flags, int startid) { super.onstartcommand(intent, flags, startid); string task_name =intent.getextras().getstring("task_name"); task_id =intent.getextras().getint("task_id"); ellapsedtime = intent.getextras().getlong("ellapsedtime"); log.d("servicebase",""+ellapsedtime); displaynotificationmessage(task_name); new thread(mythreads, new serviceworker(),"chronometerservice").start(); return start_sticky; } private class serviceworker implements runnable { public void run() { } } @override public void ondestroy() { mythreads.interrupt(); notificationmgr.cancelall(); super.ondestroy(); } public ibinder onbind(intent intent) { return null; } public void displaynotificationmessage(string message){ notification notification = new notification(r.drawable.emo_im_winking,message,system.currenttimemillis()); notification.flags = notification.flag_no_clear; intent intent = new intent(this, recentactivity.class); intent.putextra("task_id", task_id); intent.putextra("ellapsedtime",ellapsedtime); log.d("servicebase1",""+long.tostring(ellapsedtime)); pendingintent contentintent = pendingintent.getactivity(this,0,intent,0); notification.setlatesteventinfo(this,"chronometerservice",message,contentintent); notificationmgr.notify(0, notification); } }
i tried send message activity service, contains elapsed information. if started first on device (after system load) it's works right, when launch again. activity receives wrong message. receives time of first service launched on device.
as can see send 1 more variable, , activity reads correctly.
i've found solution question. it's simple. it's needed use flag(pendingintent.flag_update_current)
pendingintent contentintent = pendingintent.getactivity(this,0,intent,pendingintent.flag_update_current);
and it's work fine.
Comments
Post a Comment