一、开启新线程

new Thread(new Runnable(){  

    public void run(){  
        Thread.sleep(XXXX);  
        handler.sendMessage();----告诉主线程执行任务  
    }  
}).start  
 

  

 

二、利用定时器

TimerTask task = new TimerTask(){  
    public void run(){  
    //execute the task   
    }  
};  
Timer timer = new Timer();
timer.schedule(task, delay);

  

 

 

三、handler + postDelayed

new Handler().postDelayed(new Runnable(){  
    public void run() {  
    //execute the task
    }  
 }, delay);  

  

 

四、利用AlarmManager,特点时刻广播指定意图 能实现,一般的简单任务不这么做。

 

 

注意:不要直接在代码中用  Thread.sleep();这个方法是让当前线程延迟,不符合延迟特定代码的需求。

相关文章:

  • 2021-10-29
  • 2021-09-30
  • 2021-12-04
  • 2021-11-29
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-08
  • 2021-11-09
  • 2021-10-29
  • 2022-12-23
  • 2021-06-28
  • 2021-09-02
相关资源
相似解决方案