如何保持CountDownTimer运行时,我preSS后退按钮 [英] How keep a CountDownTimer running when I press the back button

查看:198
本文介绍了如何保持CountDownTimer运行时,我preSS后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CountDownTimer它更新一个TextView,但是当我preSS后退按钮停止。

I have a CountDownTimer which updates a TextView, but when I press the back button it stops.

CountDownTimer timer = new CountDownTimer(600000, 100) {

    public void onTick(long millisUntilFinished) {
        calendar.setTimeInMillis(millisUntilFinished);

        cron.setText(sf.format(calendar.getTime()));
    }

    public void onFinish() {
    }
};

我怎样才能保持它运行的时候我preSS后退按钮和活性被破坏?

How can I keep it running when I press the back button and the activity gets destroyed?

PS:我也启动了GPS服务时,计时器开始,有没有办法把CountDownTimer在服务

PS: I also start a GPS service when the timer starts, Is there a way to put the CountDownTimer in the service?

推荐答案

如果您的计时器需要生活超越你的活动的生命周期,那么它不属于那里。你当然可以把它放到一个服务或其他对象(如单身),可以在内存中驻留。

If your timer needs to live beyond the lifecycle of your Activity, then it does not belong there. You can certainly place it into a Service, or another object (like a singleton) that can stay resident in memory.

这篇关于如何保持CountDownTimer运行时,我preSS后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆