我想使用CountDownTimer從後臺線程更新UI線程中的文本,只計算剩餘時間。從Android中的後臺線程使用CountDownTimer
我子類一個線程,並在其構造函數傳遞的主線程處理程序,並創造了run方法新CountDownTimer到如下更新主UI線程文本視圖:
class MyCounter extends Thread
{
private Handler uiHandler;
private long timeToCount;
public MyCounter(Handler handler, long time)
{
this.uiHandler = handler;
this.timeToCount = time;
}
public void run()
{
new CountDownTimer(timeToCount, 1000){
public void onTick(long millisUntilFinished) {
Message msg = Message.obtain();
String timeString = //....here I conver millisUntilFinished to String format
msg.obj = timeString;
uiHandler.sendMessage(msg);
}
.... //Other method of CountDownTimer
}.start();
} //end of run
} //end of thread class
創建MyCounter線程,並呼籲開始我的活動UI(主線程)傳遞在主線程中創建的處理程序。在我的主要活動中調用start後,似乎CountDownTimer沒有被調用。
任何想法?