Android延时执行的几种方法

来源:互联网 发布:淘宝网怎么装修店面 编辑:程序博客网 时间:2024/05/16 10:48

原文地址:http://blog.csdn.net/hp_2008/article/details/8207286

一、开启新线程
new Thread(new Runnable(){
public void run(){
Thread.sleep(2000);
handler.sendMessage(); //告诉主线程执行任务
}
}).start
此处start为报错,原因不明。

二、利用定时器
TimerTask task = new TimerTask(){
public void run(){
//execute the task
}
};
Timer timer = new Timer();
timer.schedule(task, delay);
http://www.android-study.com/jichuzhishi/536.html

三、
new Handler().postDelayed(new Runnable(){
public void run() {
Toast.makeText(AuxInSkypine.this, "No Video Signal!", 1000).show();
finish();
//execute the task
}
}, 2000);

四、利用AlarmManager,特点时刻广播指定意图 能实现,一般的简单任务不这么做。
 
http://bbs.csdn.net/topics/390174524
 
原创粉丝点击