java - Android service onDestroy method runs on delay after stopService() called -
i using service run timer controlled using main activity. however, lines of code comes after calling stopservice()
seems run before ondestroy()
method in service.
here's code in mainactivityclass calls stopservice()
:
public void onclicklayouts(view v){ //on click layout s //behaviour stopservice(intenttimerservice); sharedpreferences.edit().putboolean("issselected", true).apply(); updatelayout(); if(sharedpreferences.getboolean("isplaying", false)){ runtimerservice(); } }
where runtimerservice()
is:
public void runtimerservice(){ sharedpreferences.edit().putint("inttimeonbegin", (int) systemclock.elapsedrealtime()).apply(); startservice(intenttimerservice); }
this ondestroy()
method in serviceclass:
@override public void ondestroy() { super.ondestroy(); //runs on service end //behaviour handler.removecallbacks(runnable); if(sharedpreferences.getboolean("issselected", true)){ sharedpreferences.edit().putint("intstime", inttime).apply(); }else{ sharedpreferences.edit().putint("intptime", inttime).apply(); } inttime = 0; }
where runnable
is:
handler = new handler(); runnable = new runnable() { @override public void run() { //runnable //behaviour if(sharedpreferences.getboolean("issselected", true)){ //runs if s selected //behaviour inttime = sharedpreferences.getint("intstime", 0) + ((int) systemclock.elapsedrealtime() - sharedpreferences.getint("inttimeonbegin", 0)); sharedpreferences.edit().putstring("stringstime", formattime(inttime)).apply(); sharedpreferences.edit().putstring("stringptime", formattime(sharedpreferences.getint("intptime", 0))).apply(); }else{ //runs if p selected //behaviour inttime = sharedpreferences.getint("intptime", 0) + ((int) systemclock.elapsedrealtime() - sharedpreferences.getint("inttimeonbegin", 0)); sharedpreferences.edit().putstring("stringptime", formattime(inttime)).apply(); sharedpreferences.edit().putstring("stringstime", formattime(sharedpreferences.getint("intstime", 0))).apply(); } //set runnable repeat interval handler.postdelayed(this, 1); } };
so presumably, once call onclicklayouts()
in mainactivity should save value of inttime
"intstime"
in sharedpreferences. however, not case, when run app , call onclicklayouts()
saves value of inttime
"intptime"
instead. why this? in advance :)
Comments
Post a Comment