1 线程
new Thread(new Runnable(){
public void run(){
Thread.sleep(XXXX);
handler.sendMessage();----告诉主线程执行任务
}
}).start
2定时器
TimerTask task = new TimerTask(){
public void run(){
//execute the task
}
};
Timer timer = new Timer();
timer.schedule(task, delay);
3handler
new Handler().postDelayed(new Runnable(){
public void run() {
//execute the task
}
}, delay);
在开发中,会遇到程序崩溃。主要是耗时操作造成的。
11-21 14:30:16.542: E/AndroidRuntime(9385): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@3f0156d is not valid; is your activity running?
解决,在ondestory中停掉耗时操作
通过handler.postDelayed(runnable, 3000);做延迟操作
在退出时,没有停掉这个线程,导致崩溃。
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
// stopConnectingToDevice();
}