2014-01-28 42 views
0

我有這個perodic 處理器我可以在運行時更改定期處理程序的時間間隔嗎?

int interval = 35000; 

Runnable runnableForConsumption = new Runnable() { 
     @Override 
     public void run() { 

      handler.postDelayed(runnableForConsumption, interval); 
      new ConnectWSTask().execute(); 
     } 
    }; 

    void startRepeatingTask() { 
     runnableForConsumption.run(); 

     } 

    void stopRepeatingTask() { 
     handler.removeCallbacks(runnableForConsumption); 
    } 

而且我想第一次運行2000毫秒的間隔,這運行後我想35000ms的時間間隔。

是否有可能在運行期間更改此處理程序的間隔?

還有沒有其他的可能性做這樣的事情?

回答

0

你可以試試這個:

public void run(){ 

     handler.postDelayed(runnableForConsumption, interval); 

     if(interval == 2000){ 
      interval = 3500; 
     } 
     new ConnectWSTask().execute(); 
    } 

的一點是你的第二postDelayed

+0

我認爲這不是在工作之前改變interval 3500。當我調用'runnableForConsumption.run();'時,run方法將被執行一次,並且一旦在postdelayed中設置了間隔,它將始終在此間隔中執行。還是我以錯誤的方式思考? –

+0

哦,好的。它真的有用。沒有那個。但它正在反其道而行之。 if語句使得後期延遲。 –

+0

編輯我的答案。當你第一次調用'runnableForConsumption.run()'時,interval是2000,在'run'方法中,間隔設置爲3500,後面跟着'postDelayed'。然後你的第二個'run'將被安排在'currentTime + 3500'。所有後續的「運行」將使用相同的延遲時間 – suitianshi

相關問題