我得到了socket.io的通知,在我的android應用程序中啓動倒數計時器。我使用Handler將數據從socket.io回調發送到UI線程。處理程序的消息完美。但從處理程序啓動倒計時器不起作用(不會調用onTick()函數)。如果我用UI元素啓動它,一切都會好的。當倒數計時器不是從UI線程操作時,最好的方法是什麼?從處理程序開始倒數計時器的問題
mHandler = new Handler(){
public void handleMessage(Message inputMessage) {
mBattle = (Battle) inputMessage.obj;
switch (inputMessage.what) {
case NO_BATTLE:
System.out.println("got message NO_BATTLE");
break;
//.................
case START_BATTLE:
startCountdownTimer(mBattle.getCountdown());
System.out.println("got message START_BATTLE");
break;
}
}
和倒數計時功能:
private void startProgressBar(final int time){
if (time != 0) {
new CountDownTimer(time, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress " + i + " " +
+ millisUntilFinished);
i++;
}
@Override
public void onFinish() {
i=0;
}
}.start();
}
使用mHandler而不是CountDownTimer來計數diwn – pskink