multithreading - How to send data from local service to activity in ANDROID -
i have 1 activity , 1 service. requirement start service activity , in activity countdown timer start, problem i'm unable value service activity. please me out.
is code,tutorial,example me this.
timer service
public class timerservice extends service { mycounter timer; @override public void oncreate() { timer = new mycounter(1 * 60 * 1000, 1000); super.oncreate(); } @override public int onstartcommand(intent intent, int flags, int startid) { timer.start(); return super.onstartcommand(intent, flags, startid); } private class mycounter extends countdowntimer { public mycounter(long millisinfuture, long countdowninterval) { super(millisinfuture, countdowninterval); } @override public void onfinish() { toast.maketext(getapplicationcontext(), "death", toast.length_long) .show(); stopself(); } @override public void ontick(long millisuntilfinished) { toast.maketext(getapplicationcontext(), (millisuntilfinished / 1000) + "", toast.length_short) .show(); } } @override public void ondestroy() { // todo auto-generated method stub timer.cancel(); super.ondestroy(); } @override public ibinder onbind(intent arg0) { return null; } }
what should write in service toast message value in activity
read broadcast receivers.
create broadcast receiver in activity , register intentfilter(set action in string value).
private broadcastreceiver receiver = new broadcastreceiver() { public void onreceive(android.content.context context, intent intent) { toast.maketext(context, "death", toast.length_long) .show(); }}
register in onresume
intentfilter intentfilter = new intentfilter(); intentfilter.addaction("your action"); this.registerreceiver(receiver, intentfilter);
and in service call setbroadcast
@override public void onfinish() { intent intent = new intent(); intent.setaction("youraction"); sendbroadcast(intent); stopself(); }
Comments
Post a Comment