我有一個非常簡單的活動,它將自定義日期時間字符串輸出到textView。我在MainActivity中自己實現了一個可運行接口,並在onCreate
方法的末尾創建了Thread
。在MainActivity中實現Runnable
Thread thread = new Thread(this);
thread.run();
這是run方法
public void run() {
while (true) {
try {
Thread.sleep(1000);
localTime.setText(LocalTime.getTime().format2445());
System.out.println(LocalTime.getTime().format2445());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
當我在模擬器上我可以看到系統出logcat中運行它。但仿真器在約5秒後顯示ANR。我究竟做錯了什麼。這種方法不適合嗎?異步任務是這裏的路嗎?我需要每秒更新一次時鐘,所以AsyncTask永遠運行是可接受的模式?
你需要調用。 'thread.start()'。 – Raghunandan
你想做什麼?實現一個'CountDownTimer'? – gunar
也,'localTime'是一個'TextView'? – gunar