android - how do i make a timer continue counting down after the app stopped? -
i have class has method has timer within it. when method called timer starts , goes on forever when app closed stops , not count downwards when app reopened. there way check if timer ticking before , create tick shared preferences or something? dont know.
so how create timer go on counting downwards after app stopped?
method below
public int shipadd() { if (counter >= addspend) {
counter -= addspend; addspend += addspend; counterpersec +=addamount; addclick += addclick; test++; // count downwards timer below new timerclass(addtime, 1000) { public void onfinish() { counter += addamount; this.start(); } }.start(); } else if (counter < addspend) { } homecoming addspend;
}
a great way using service.
public class countdowntimer extends service{ @override public ibinder onbind(intent intent) { // todo auto-generated method stub homecoming null; } @override public void oncreate() { //enter timer code here } }
in class want start service,use start service.
in activity:
startservice(new intent(getapplicationcontext(), countdowntimer.class));
in fragment
getactivity().startservice(new intent(getactivity(), countdowntimer.class));
update manifest line
<service android:name=".countdowntimer" ></service>
android timer
No comments:
Post a Comment