android - 可运行和动画停止 Android Studio JUnit 测试
问题描述
我有多个 JUnit 测试单击触发这些功能的按钮:
public void startTimer() {
startTime = SystemClock.uptimeMillis();
handler.post(runTimer);
blink();
}
Runnable runTimer = new Runnable() {
@Override
public void run () {
millisecondTime = SystemClock.uptimeMillis() - startTime;
updateTime = bufTime + millisecondTime;
Seconds = (int) (updateTime / 1000);
Minutes = (int) (updateTime / 60000);
Hours = Minutes / 60;
Minutes = Minutes % 60;
Seconds = Seconds % 60;
MilliSeconds = (int) (updateTime % 1000);
timer.setText(String.format("%02d", Hours) + ":" + String.format("%02d", Minutes) + ":"
+ String.format("%02d", Seconds));
handler.postDelayed(this, 0);
}
public void blink() {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(250); //You can manage the blinking time with this parameter
anim.setStartOffset(0);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
redCircle.startAnimation(anim);
}
对于上下文,我的 JUnit 测试的下一步单击调用停止动画和 handler.removeCallbacks(runTimer) 的按钮。但是它甚至没有到达那里,因为它只是卡住了!
当我在 startTimer() 函数中同时注释掉 handler.post() 和 blink() 时,JUnit 测试再次开始工作。我似乎找不到任何解决方案。有人可以帮忙吗?
解决方案
我修好了这个!
我做了多项更改,我认为这是主要更改:
handler.postDelayed(this, 0);
至
handler.postDelayed(this, 1000);
也不确定它是否有帮助,但万一我也这样做了:
handler.post(runTimer);
至
runOnUiThread(runTimer);