android中延迟执行某个任务

来源:互联网 发布:使命召唤fa7.62l数据 编辑:程序博客网 时间:2024/04/29 21:05

android App开发在某些情况下需要有延时功能,比如说App首页显示定格3秒,然后自动跳到登录页的情况,这就好比是一个预加载,但是这个预加载可能瞬间就完成了,撑不到3秒钟,这是就要求你做延时处理。

下面是三种方法:

一、线程
[html] view plaincopyprint?
  1. 1. new Thread(new Runnable(){    
  2. 2.     public void run(){    
  3. 3.         Thread.sleep(XXXX);    
  4. 4.         handler.sendMessage();----告诉主线程执行任务    
  5. 5.     }    
  6. 6. }).start    
二、延时器
[html] view plaincopyprint?
  1. 1. TimerTask task = new TimerTask(){    
  2. 2.     public void run(){    
  3. 3.     //execute the task     
  4. 4.     }    
  5. 5. };    
  6. 6. Timer timer = new Timer();  
  7.    timer.schedule(task, delay);  
三、android消息处理
[html] view plaincopyprint?
  1. new Handler().postDelayed(new Runnable(){    
  2.     public void run() {    
  3.     //execute the task    
  4.     }    
  5.  }, delay);   

推荐使用第三种
0 0
原创粉丝点击