Android延時執行呼叫的幾種方法(轉)
阿新 • • 發佈:2019-01-23
From: http://blog.sina.com.cn/s/blog_46726d2401015t8q.html
一、開啟新執行緒
new Thread( new Runnable()
public void run()
Thread.sleep(XXXX);
handler.sendMessage(); //告訴主執行緒執行任務
}
}).start
|
- new Thread(new Runnable()
- {
- publicvoid run()
- {
- Thread.sleep(XXXX);
-
handler.sendMessage();----告訴主執行緒執行任務
- }
- }).start
二、利用定時器
- TimerTask task = new TimerTask()
- {
- publicvoid run()
- {
- //execute the task
- }
- };
- Timer timer = new Timer();timer.schedule(task,delay);
三、handler + postDelayed
- new Handler().postDelayed(new Runnable()
- {
- publicvoid run()
- {
-
//execute the task
- }
- }, delay);
四、利用AlarmManager,特點時刻廣播指定意圖 能實現,一般的簡單任務不這麼做。