我正在设计适用于Android 2.2的计时器/倒数计时器应用程序,并希望按一下按钮同时启动计时器和计时器。所以,理想情况下,我希望上的秒(时间)计时器和计时器在同一个实例中更改。 (即使计时器正在计数,计时器也会倒计时)。由于我使用Android提供的计时器和计时器功能,当用户按下“开始”按钮时,我编写了以下代码
private boolean mStartPressedOnce = false;
long mTimeWhenStopped = 0;
Chronometer mChronometer;
MyCounter mCounter;
...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:
// Perform some initialization for the chronometer depending
// on the button press
if (mStartPressedOnce == false) {
mChronometer.setBase(SystemClock.elapsedRealtime());
} else {
mChronometer.setBase(SystemClock.elapsedRealtime() + mTimeWhenStopped);
}
// Perform the initialization for the timer
mCounter = new MyCount(45000, 1000);
// Fire up the chronometer
mChronometer.start();
// Fire up the timer
mCounter.start();
break;
case R.id.ResetButton:
// Reset the chronometer
mChronometer.setBase(SystemClock.elapsedRealtime());
mTimeWhenStopped = 0;
break;
case case R.id.StopButton:
mStartPressedOnce = true;
// Stop the chronometer
mTimeWhenStopped = mChronometer.getBase() - SystemClock.elapsedRealtime();
mChronometer.stop();
break;
}
...
public class MyCounter extends CountDownTimer {
@Override
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
// Nothing to do here
}
@Override
public void onTick(long millisUntilFinished) {
long seconds = (long) (millisUntilFinished / 1000);
long minutes = (long) ((millisUntilFinished / 1000) / 60);
long hours = (long) (((millisUntilFinished / 1000) / 60) / 60);
// Do some formatting to make seconds, minutes and hours look pretty
// Update the timer TextView
(TextView) findViewById(R.id.CountDownTimerTextView))
.setText(hours + ":" + minutes + ":" + seconds);
}
}
虽然看起来计时器和计时器上的秒数最初是同步,但在短时间后,它们似乎会熄灭,两者的第二次更新都会在不同的时间发生。
想知道我能做些什么来解决这个问题。我确实遇到过 - 并阅读了这个帖子
Running multiple AsyncTasks at the same time -- not possible?
我意识到可能需要进行设计更改,但我不确定需要做什么。
编辑:计时器和计时器的包含类型以及使用天文台计算时间的方法 - 每个jolivier和njzk2的建议
答案 0 :(得分:2)
您可以使用System.currentTimeMillis()检索当前时间,将其存储到变量中并将其转发到mChronometer
和mCounter
,以便它们使用相同的时间参考,尽管它们的任务已启动在不同的时间。
编辑:对于给定的类型,android documentation about Chronometer会告诉您可以使用elapsedRealTime来实现我所说的内容。
CountDownTimer没有这个,其start
方法是最终的,所以您可能想要使用其他实现,更好地了解您的用例可能对我们有所帮助。
基本上,想要两个线程在同一毫秒内执行操作绝不是一个好主意,其中一个将作为时钟,另一个必须是从属并听取时钟。
答案 1 :(得分:2)
所以,在仔细考虑了一段时间之后,在与我们慷慨分享的建议jolivier
之后,我意识到存在一种名为onChronometerTick
的方法,每当有计时器时就会调用它tick(在这种情况下每秒一次)。因此,我想每次调用该方法时从计数器中减去1000毫秒,并相应地更新计时器显示。我完全摆脱了Android计时器片(CountDownTimer
)。我认为这将是同时更新两个显示器的好方法。它也是一个简单的计时器实现。
我很高兴地报告它似乎运作良好。计时器和计时器显示器确实同时更新。所以,原来的问题看起来像是回答了。不幸的是,我在计时器前端遇到了一个两个错误的错误,我用一个丑陋的黑客修复了。我发布了迄今为止的内容。欢迎任何有关如何修复黑客或改进代码的建议。请注意,我已对代码进行了评论,以便更容易理解已完成的操作。
编辑bug:我注意到的另一件事是大约10分钟左右后,计时器和计时器关闭了一秒钟。更确切地说,计时器在计时器后面一秒钟。还不确定为什么会这样。
private boolean mStartPressedOnce = false;
long mTimeWhenStopped = 0;
Chronometer mChronometer;
long millisUntilFinished = 0;
boolean firstPassOver = false;
int counter = 0;
...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:
// Perform some initialization for the chronometer depending
// on the button press
if (mStartPressedOnce == false) {
mChronometer.setBase(SystemClock.elapsedRealtime());
} else {
mChronometer.setBase(SystemClock.elapsedRealtime() + mTimeWhenStopped);
}
// Fire up the chronometer
mChronometer.start();
break;
case R.id.ResetButton:
// Reset the chronometer
mChronometer.setBase(SystemClock.elapsedRealtime());
mTimeWhenStopped = 0;
break;
case case R.id.StopButton:
mStartPressedOnce = true;
// Stop the chronometer
mTimeWhenStopped = mChronometer.getBase() - SystemClock.elapsedRealtime();
mChronometer.stop();
break;
}
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stop_watch);
mChronometer = (Chronometer) findViewById(R.id.StopWatchTextView);
// Initialize the number of milliseconds before the timer expires (
// set the timer) - in this case to 46 seconds
millisUntilFinished = 46000;
// Display how many seconds remain before the timer expires
((TextView) findViewById(R.id.CountDownTimerTextView)).setText(hours
+ ":" + minutes + ":" + millisUntilFinished / 1000);
// In line with the suggestion provided by jolivier - make the timer
// the slave and update its display every time the chronometer
// ticks
mChronometer
.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
// Update the display for the chronometer
CharSequence text = chronometer.getText();
chronometer.setText(text);
// Update the display for the timer
// !!! BUG !!!
// Looks like onChronometerTick is called at the 0th second
// and this causes an off by two error if a count down timer
// is being implemented. Fixed it with this hack. There's gotta
// be a more elegant solution, though.
if(counter >= 2) {
millisUntilFinished1 = millisUntilFinished1 - 1000;
counter = 2;
}
counter++;
if (millisUntilFinished >= 0) {
long seconds = (long) (millisUntilFinished / 1000);
long minutes = (long) ((millisUntilFinished / 1000) / 60);
long hours = (long) (((millisUntilFinished / 1000) / 60) / 60);
// Do some formatting to make seconds, minutes and hours look pretty
// Update the timer TextView
((TextView) findViewById(R.id.CountDownTimerTextView))
.setText(hours + ":" + minutes + ":"
+ seconds);
}
}
});
// Other code
...
}