2013-08-01 30 views

回答

0

您需要使用TimerTask

例如:如果你希望儘快活性/用戶窗體加載啓動定時器,併成爲對用戶可見,你可以在onStart()方法你的活動添加下面的代碼:

int timer_wait_time= 5000; // in miliseconds 
Timer timer = new Timer(); 
TimerTask myTT= new MyTimerTask(); 
timer.schedule(myTT, timer_wait_time); 

這將啓動定時器whic h將在timer_wait_time通過後過期。它會執行TimerTask的run()方法(這裏是「MyTimerTask」)。

MyTimerTask的定義是這樣的:

class MyTimerTask extends TimerTask{ 

      // this method gets called when the desired time is over 
     public void run() { 
      // put your code here about what you want to do when the timer expires. 
      // maybe code to switch to other activity or disabling the text field 
     } 

} 

我希望幫助你。