延迟执行可以通过以下操作实现,按照推荐的顺序列出来
1.
2.
1.
new Handler().postDelayed(new Runnable(){ public void run() { //execute the task } }, delay);
mHandler.sendEmptyMessageDelayed(WIFI_GPS_STATE_ON, 4 * 1000);
3.利用定时器
4.开启新线程
5.利用AlarmManager,特点时刻广播指定意图 能实现,一般的简单任务不这么做。
TimerTask task = new TimerTask(){ public void run(){ //execute the task } }; Timer timer = new Timer(); timer.schedule(task, delay);
new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主线程执行任务 } }).start