Android Chronometer从定义的值开始 [英] Android Chronometer start with defined value

查看:242
本文介绍了Android Chronometer从定义的值开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中向用户显示计时器,以显示用户的工作状况. 每当活动回到后台时(无论是通过主页按钮还是返回主页),我都会节省该时间(以秒为单位),而当活动返回时,我希望继续从同一时间开始运行计时码表.用户可能会从列表中选择其他项目,并且时间有所不同,并且他可能会关闭电话... 我可以保存天文钟的时间,但是不能设置开始时间.

I have an application in which I'm displaying a chronometer to the user for what he's doing. Whenever the activity goes to the background (wether by home button, or back) I save that time (in seconds) and when the activity is brought back, I want to continue the chronometer running from the same time. The user might select a different item from the list, and the time is different, and also he might turn off the phone... I can save the time of the chronometer, however I can't set it with a start time.

测风头API 中,方法setBase()指出:

设置倒数计时器的参考时间.

Set the time that the count-up timer is in reference to.

据我了解,这意味着,如果将此值设置为currentTime,它将开始以0计数. 现在,如果我希望它以值17s开头,我考虑过将基数设置为currentTime减去17秒前的时间.像这样:

On my understanding, this means that if I set this value to currentTime, it'll start counting with 0. Now, if I want it to start with the value 17s I thought about setting the base to the currentTime less the time 17 seconds ago. So something like:

setBase(system.currentTimeMillis() - (17 * 1000)).

但是这不起作用,并且总是以0开始!

However this doesn't work, and the it starts always with 0!

我在这里阅读了其他一些主题,但所有答案都无济于事.它总是以0开头!

I read a few other threads around here, and none of the answers helped. It always starts with 0!

谢谢.

推荐答案

我认为您将不得不自己跟踪一些时间标记.

I think you're going to have to keep track of some time marks yourself.

Chronometer myChrono;
long baseTime;
long stopTime;
long elapsedTime;

设置基准时间后,您要使用:

When you set the base time, you want to use:

baseTime = SystemClock.elapsedRealtime() - stopTime;
myChrono.setBase(baseTime);

要查看已过去了多少时间,请使用:

When you want to see how much time has passed, use:

elapsedTime = SystemClock.elapsedRealtime() - myChrono.getBase();

看看SystemClock doc .

Take a look at the SystemClock doc.

这篇关于Android Chronometer从定义的值开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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