我已经看到这个问题在搜索时弹出了几次,但似乎没有解决我的问题。我的倒数计时器在一个服务中运行并且工作得很好,除了它在到达onFinish()之前有一个延迟。它将在完成()上进行5,4,3,2,1(长暂停)。然而,这个问题;当我在我的设备上使用android studio的调试器模式时不会发生。当设备未连接到调试器时,我的应用程序会遇到延迟。有没有办法在使用倒数计时器时删除修复问题?我看到的其他解决方案涉及使用处理程序或可运行程序,但我更喜欢用倒数计时器说。下面的代码包含服务中的倒数计时器。我感谢任何帮助。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
if (startService) {
startService = false;
data = intent.getIntExtra("the", 0);
// int progress = data/1000;
// CountDownAct.timeProgressBar.setMax(progress);
//
// CountDownAct.timeProgressBar.setProgress(1000);
countDownTimer = new CountDownTime(data, 1000);
countDownTimer.start();
// timer = new Timer();
// IntentFilter filter = new IntentFilter();
// filter.addAction(ALARM_SERVICE);
// registerReceiver(timer, filter);
}
}
}
public class CountDownTime extends CountDownTimer {
/**
* @param millisInFuture
* The number of millis in the future from the call to
* {@link #start()} until the countdown is done and
* {@link #onFinish()} is called.
* @param countDownInterval
* The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTime(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
int progress = (int) (millisUntilFinished / 1000);
// this is the format which reads the time data from the user and
// makes it readable
hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
CountDownAct.timeTextView.setText(hms);
CountDownAct.timeProgressBar.setProgress(progress);
Log.i("CountDownService", hms);
}
@Override
public void onFinish() {
CountDownAct.timeProgressBar.setProgress(0);
Intent goBack = new Intent(getApplicationContext(), alarmtimefinished.class);
goBack.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
goBack.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(goBack);
// CountDownService.startService = true;
//
}
}
答案 0 :(得分:0)
试了一下,非常奇怪..呃,我不确定这是不是真的是一个'答案',但是解决方案就是忘记onFinish并创建自己的onfinish with like(millisUntilFinished / 1000 == 1){// ..做任何事情}
答案 1 :(得分:0)
尝试用100而不是1000
来调用计时器countDownTimer = new CountDownTime(data, 100);