首先內repeatingly運行一個線程,我甚至不能選擇使用,我讀了好幾個小時,現在,有人說用「處理程序」,有人說用「定時器」的方法。這是我儘量做到:Android的 - 定時器
在喜好,那裏有一個設置(複選框)的啓用/禁用的重複工作。作爲該複選框被選中時,計時器應開始工作和線程應每x秒執行。由於複選框未選中,計時器應停止。
這裏是我的代碼:
檢查時是否複選框被選中與否,如果選中「refreshAllServers」無效將被執行,不帶定時工作。
boolean CheckboxPreference = prefs.getBoolean("checkboxPref", true);
if(CheckboxPreference == true) {
Main main = new Main();
main.refreshAllServers("start");
} else {
Main main = new Main();
main.refreshAllServers("stop");
}
的refreshAllServers無效,做計時器作業:
public void refreshAllServers(String start) {
if(start == "start") {
// Start the timer which will repeatingly execute the thread
} else {
// stop the timer
}
這裏就是我如何執行我的主題:(工作良好,沒有定時器)
Thread myThread = new MyThread(-5);
myThread.start();
我試過嗎?
我嘗試過的例子,我可以看到從谷歌(處理器,計時器)沒有人的工作,我設法一旦啓動計時器,但回採沒有奏效。 的simpliest &理解的代碼,我在我的研究中看到的是這樣的:
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
// your code here
}
},
5000
);
看看答案[這裏](http://stackoverflow.com/questions/8098806/where-do-i-create-and-use-scheduledthreadpoolexecutor-timertask-or-handler/8102488#8102488)幫助。 – yorkw 2012-07-24 21:26:26