基本上這是我在做什麼,而不是使用Timer
我使用的是Handler
:處理程序而不是定時器?
Handler h = new Handler();
Runnable r = new Runnable(){
public void run(){
//do something and schedule it again
h.postDelayed(r, 10000);
}
};
我說h.removeCallBacks(r);
時,我想阻止它。它不好嗎?
基本上這是我在做什麼,而不是使用Timer
我使用的是Handler
:處理程序而不是定時器?
Handler h = new Handler();
Runnable r = new Runnable(){
public void run(){
//do something and schedule it again
h.postDelayed(r, 10000);
}
};
我說h.removeCallBacks(r);
時,我想阻止它。它不好嗎?
不好。
不(除非你有一些問題:))。 不要忘記在方法中取消Runnable
,這樣您可以取消正在運行的任何待定Runnable
可能的死亡活動。
處理程序有兩個主要用途:(1)安排消息和 將作爲將來的某個點執行;和(2)排隊 要在不同於你自己的線程上執行的動作。
向下選民請添加評論... – ngesh 2013-03-01 10:31:43
我認爲這是正確的方法。 – njzk2 2013-03-01 10:34:03
當你不想再使用它時,你可以在刪除回調後添加一個東西,h = null; – 2013-03-01 11:06:34