旋转后恢复CountdownTimer [英] Resuming CountdownTimer after rotation

查看:146
本文介绍了旋转后恢复CountdownTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个统计从60秒下来CountdownTimer。这CountdownTimer的工作原理是一个TextView设置为剩余毫秒,但每当我转动我的设备中,CountdownTimer被复位。

我知道这是因为活动被轮换重新启动。于是,我就保存在剩余捆绑,然后还原它,重新启动活动后的时间。

 长transferValue;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_playtimemode);    Log.d(调试,的onCreate:+ transferValue);    长的setTime = 60000;
    长差=的setTime - transferValue;    新CountDownTimer(差值,1000){        公共无效onTick(长millisUntilFinished){
            millisUntilFinishedToSave = millisUntilFinished;
            tvCountdown.setText(+ millisUntilFinished / 1000);
        }        公共无效onFinish(){
            tvCountdown.setText(完成了!);
        }
    }。开始();}@覆盖
保护无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    outState.putLong(millisKey,millisUntilFinishedToSave);}@覆盖
保护无效onRestoreInstanceState(捆绑savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    transferValue = savedInstanceState.getLong(millisKey);
    Log.d(调试,onRestoreInstanceState():+ transferValue);
}

然而,这并不正常工作。我在这个code(因此它返回0)的顶部intializing transferValue,但如何我可以从其他的savedInstanceState到CountdownTimer?

保存数据

  20 07-06:21:30.038:D /调试(28995)的onCreate:0
07-06 20:21:30.043:D /调试(28995):onRestoreInstanceState():55994


解决方案

我想给你的计时器它自己的线程。您的计时器被停止,因为它是在UI线程上(如你所述),当用户界面重绘活动是重新初始化。所有长时间运行的进程应该有自己的线程。经验法则:尽快走出UI线程

下面是使用服务的一个例子。所谓的,留在记忆,无论屏幕方向改变,甚至活动重点的变化时,该服务将启动。

下面是该服务:

 公共类定时器延伸服务{    @覆盖
    公众的IBinder onBind(意向我){
        返回null;
    }
    @覆盖
    公众诠释onStartCommand(意向我,诠释标志诠释startId){
        //把你的计时器code在这里
    }
}

您需要将此行添加到您的清单(应用程序之间的某处开启/关闭标签):

 <服务机器人:名字=定时器/>

然后,您可以使用此随时开始服务(需要注意的是,你可以在启动它一遍又一遍,递归调用不作新的服务是很重要的。)

  startService(新意图(这一点,Timer.class));

I have a CountdownTimer that counts down from 60 seconds. This CountdownTimer works by setting a textView to the remaining milliseconds, but whenever i rotate my device, the CountdownTimer gets reset.

I know this happens because the Activity gets restarted on rotation. So i tried saving the time remaining in a bundle and then restoring it, after the Activity was restarted.

long transferValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playtimemode);

    Log.d("Debug", "onCreate: " + transferValue);

    long setTime = 60000;
    long difference = setTime - transferValue;

    new CountDownTimer(difference, 1000) {

        public void onTick(long millisUntilFinished) {
            millisUntilFinishedToSave = millisUntilFinished;
            tvCountdown.setText("" + millisUntilFinished / 1000);
        }

        public void onFinish() {
            tvCountdown.setText("done!");
        }
    }.start();

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putLong("millisKey", millisUntilFinishedToSave);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    transferValue = savedInstanceState.getLong("millisKey");
    Log.d("Debug", "onRestoreInstanceState(): " + transferValue);
}

This however doesn't work. I am intializing transferValue at the top of this code (hence it returning 0), but how can i else save the data from the savedInstanceState to the CountdownTimer?

07-06 20:21:30.038: D/Debug(28995): onCreate: 0
07-06 20:21:30.043: D/Debug(28995): onRestoreInstanceState(): 55994

解决方案

I would give your timer it's own thread. Your timer is being stopped because it's on the UI thread (as you stated) and when the UI redraws the Activity is re-initialized. All long running processes should have their own thread. Rule of thumb: get out of the UI thread as soon as possible.

Here is an example of using a Service. This service will start when called and stay in memory regardless of screen orientation changes or even activity focus changes.

Here is the service:

public class Timer extends Service {

    @Override
    public IBinder onBind(Intent i) {
        return null;
    }
    @Override
    public int onStartCommand(Intent i, int flags, int startId) {
        // Put your timer code here
    }
}

You will need to add this line to your manifest (somewhere between the application open/close tags):

<service android:name=".Timer" />

Then you can start the service at any time by using this (it's important to note that you can start it over and over again, the recursive calls do not make a new service.):

startService(new Intent(this, Timer.class));

这篇关于旋转后恢复CountdownTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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