-->

Countdown timer stops running in background

2019-06-09 18:53发布

问题:

I have implemented a countdown timer in an application of mine. It runs in the background fine and dandy, but when i use advanced task killer, it stops the timer and the only way to restart it is to open the application again. Is there anyway to have the timer persist, even if I use something like advanced task killer?

Code:

    TextView tv;
    final MyCounter timer = new MyCounter(10000,1000);

    tv  = (TextView)findViewById(R.id.healthtext);
    tv.setText("10"); 
    timer.start();
}

public 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();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        tv.setText((millisUntilFinished/1000)+"");

回答1:

as far as I know - nope, since task killers destroy your app's process causing any running threads to exit



回答2:

Not while the timer is part of your application. You can of course make a timer that is not part of the application.