android - BroadcastService restart itself when app killed -
i using this broadcast service countdown timer. when kill app, not ticking on same time(second), starts again.
here code.
broadcastservice:
package com.example.emre.denemeprojesi; import android.app.service; import android.content.intent; import android.os.countdowntimer; import android.os.ibinder; import android.util.log; public class broadcastservice extends service { private final static string tag = "broadcastservice"; public static final string countdown_br = "com.example.emre.denemeprojesi"; intent bi = new intent(countdown_br); countdowntimer cdt = null; @override public void oncreate() { super.oncreate(); log.i(tag, "starting timer..."); system.out.println("starting timer"); cdt = new countdowntimer(60000, 1000) { @override public void ontick(long millisuntilfinished) { log.i(tag, "countdown seconds remaining: " + millisuntilfinished / 1000); bi.putextra("countdown", millisuntilfinished); sendbroadcast(bi); } @override public void onfinish() { log.i(tag, "timer finished"); system.out.println("timer cancelled"); } }; cdt.start(); } @override public void ondestroy() { super.ondestroy(); } @override public int onstartcommand(intent intent, int flags, int startid) { return super.onstartcommand(intent, flags, startid); } @override public ibinder onbind(intent arg0) { return null; } }
main activity:
package com.example.emre.denemeprojesi; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.widget.textview; public class mainactivity extends appcompatactivity { private string tag ="countdown servisi"; private textview lblresult ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lblresult = (textview)findviewbyid(r.id.lbl_result); startservice(new intent(this, broadcastservice.class)); log.i(tag, "started service"); } private broadcastreceiver br = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { updategui(intent); // or whatever method used update gui fields } }; @override public void onresume() { super.onresume(); registerreceiver(br, new intentfilter(broadcastservice.countdown_br)); log.i(tag, "registered broacast receiver"); } @override public void onpause() { super.onpause(); unregisterreceiver(br); log.i(tag, "unregistered broacast receiver"); } @override public void onstop() { try { unregisterreceiver(br); } catch (exception e) { // receiver stopped in onpause() } super.onstop(); } @override public void ondestroy() { super.ondestroy(); // stopservice(new intent(this, broadcastservice.class)); //log.i(tag, "stopped service"); } private void updategui(intent intent) { if (intent.getextras() != null) { long millisuntilfinished = intent.getlongextra("countdown", 0); system.out.println(millisuntilfinished); //<<<bu göstermiyor<<<<<< log.i(tag, "countdown seconds remaining: " + millisuntilfinished / 1000); // } } }
Comments
Post a Comment