Android:旋转后如何还原已停止计时码表的状态? [英] Android: How to restore the state of a stopped chronometer after rotation?

查看:192
本文介绍了Android:旋转后如何还原已停止计时码表的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用这种方式保存状态并在计时器运行时旋转后恢复状态. Android_Chronometer暂停

I'm doing this way to save the state and restore it after rotation when the chronometer is running. Android_Chronometer pause

当我在Xsec处停止计时器,然后在Y秒之后,我改变了方向,天文台将标记X + Y秒. 无论走了多少时间,我都希望将计时器停止运转的时间保持在旋转之前的状态.我该怎么办?

When I stop the timer at Xsec and then after Y seconds I change the orientation the chronometer marks X+Y seconds. I'd like to leave the time of a stopped chronometer as it was before rotation, no matter how much time has passed. How should I do?

推荐答案

我对Chronometer类以及如何适应方向变化也有类似的疑问.尽管有几个有用的帖子和示例,但我发现没有一个解决了整个问题.

I had a similar question about the Chronometer class and how to survive orientation changes. While there are several useful posts and examples, none that I found addressed the total question.

这是一个很有帮助的帖子, Android_Chronometer暂停,它有助于说明需要保存经过的时间以便恢复时间.

This was a helpful post here Android_Chronometer pause, which helped with demonstrating the need to save the elapsedTime in order to resume timing.

但是,本文并未讨论如何使Chronometer幸免于Android生命周期方向变化.在计时器运行与暂停之间,经过时间的处理方式略有不同.

However, this did not discuss how to make the Chronometer survive Android life cycle orientation changes. What you do with the elapsed time is slightly different between when the timer is running vs. when it is paused.

在这里,我确实将所有内容放在一起-暂停,恢复,重设,在一个不错的班级中以及幸存下来的方向:

Here is that I did to put it all together - pausing, resuming, resetting, in a nice class, and surviving orientation:

 public class ChronometerWithPause extends Chronometer {
    private long timeWhenStopped = 0;
    private boolean isRunning = false;

    private final String getTimeKey() {
        return "KEY_TIMER_TIME" + getId();
    }
    private final String getIsRunningKey() {
        return "KEY_TIMER_RUNNING" + getId();
    }

    public ChronometerWithPause(Context context) {
        super(context);
    }

    public ChronometerWithPause(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChronometerWithPause(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void start() {
        setBase(SystemClock.elapsedRealtime() - timeWhenStopped);
        isRunning = true;
        super.start();
    }

    @Override
    public void stop() {
        isRunning = false;
        timeWhenStopped = SystemClock.elapsedRealtime() - getBase();
        super.stop();
    }

    public void reset() {
        stop();
        isRunning = false;
        setBase(SystemClock.elapsedRealtime());
        timeWhenStopped = 0;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public long getCurrentTime() {
        return timeWhenStopped;
    }

    public void setCurrentTime(long time) {
        timeWhenStopped = time;
        setBase(SystemClock.elapsedRealtime() - timeWhenStopped);
    }

    public void saveInstanceState(Bundle outState) {
        if (isRunning) {
            timeWhenStopped = SystemClock.elapsedRealtime() - getBase();
        }
        outState.putLong(getTimeKey(), getCurrentTime());
        outState.putBoolean(getIsRunningKey(), isRunning());
    }

    public void restoreInstanceState(Bundle inState) {
        isRunning = inState.getBoolean(getIsRunningKey());
        setCurrentTime(inState.getLong(getTimeKey()));
        timeWhenStopped = SystemClock.elapsedRealtime() - getBase();
        if (isRunning) {
            super.start();
        }
    }
}

请注意,您可以像这样在onSaveInstanceState()onCreate()中使用它:

Note that you can use this in onSaveInstanceState() and onCreate() like this:

protected void onSaveInstanceState(Bundle outState) {
           ...
           timer.saveInstanceState(outState);
           ...

然后在onCreate中,您可以使用以下方法恢复计时器功能:

and then in onCreate you can restore the timer function with:

if (savedInstanceState != null) {
             // . . .
                timer.restoreInstanceState(savedInstanceState);
             // . . .
}

这篇关于Android:旋转后如何还原已停止计时码表的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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