android中延遲執行某個任務

TopCoder.NET發表於2015-11-12

android App開發在某些情況下需要有延時功能,比如說App首頁顯示定格3秒,然後自動跳到登入頁的情況,這就好比是一個預載入,但是這個預載入可能瞬間就完成了,撐不到3秒鐘,這是就要求你做延時處理。

下面是三種方法:

一、執行緒

   1. new Thread(new Runnable(){  
   2.     public void run(){  
   3.         Thread.sleep(XXXX);  
   4.         handler.sendMessage();----告訴主執行緒執行任務  
   5.     }  
   6. }).start  

二、延時器

   1. TimerTask task = new TimerTask(){  
   2.     public void run(){  
   3.     //execute the task   
   4.     }  
   5. };  
   6. Timer timer = new Timer();
      timer.schedule(task, delay);

三、android訊息處理

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


推薦使用第三種

相關文章