Countdown timer stops running in background

2019-06-09 18:26发布

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)+"");

2条回答
爷、活的狠高调
2楼-- · 2019-06-09 19:07

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

查看更多
乱世女痞
3楼-- · 2019-06-09 19:09

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

查看更多
登录 后发表回答