2012-03-24 57 views
2

我有這段代碼,我想運行Log.d每1000 milis,但它只運行一次。爲什麼Handler,Timer只能運行一次?

seekView.postDelayed(new Runnable() { 

       public void run() { 
        Log.d("WWWW", "www"); 

       } 
      }, 1000); 

創建處理程序,計時器......只能像這樣運行一次,我的問題在哪裏?

回答

5

使其保持在1秒的時間間隔進行連續運行,則需要調用postDelayed嵌套在再次運行方法。看下面的例子:

seekView.postDelayed(new Runnable() { 

    public void run() { 
    Log.d("WWWW", "www"); 

    //calling postdelayed again 
    seekView.postDelayed(this, 1000);  //added this line 
    } 
}, 1000); 

這樣做會保持它以1秒的間隔自我調用。

3

使用重複:

... 
class YourTimeTask extends TimerTask { 
    public void run() { 
    .... 
    } 
} 

... 
new Timer().scheduleAtFixedRate(new YourTimerTask(), after, interval); 
...