Android延时执行的几种方法

来源:互联网 发布:js调用浏览器分享接口 编辑:程序博客网 时间:2024/05/16 04:37
一、开启新线程
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://blog.csdn.net/ordinaryjoe/article/details/8207286
原创粉丝点击