2013-03-05 32 views
4

我有一系列postDelayed處理程序。我無法設置一個停止處理程序的方法,當用戶在任何他想要的時間點擊停止按鈕時。如何停止系列postDelayed處理程序

我會感謝任何人能夠提供幫助。 感謝

while (!lessonIsRunning) { 
     Handler handler0 = new Handler(); 
     handler0.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       plate1.setVisibility(ImageView.VISIBLE); 
       plate2.setVisibility(ImageView.VISIBLE); 
       plate3.setVisibility(ImageView.VISIBLE); 
      } 
     }, 6000); 

     Handler handler1 = new Handler(); 
     handler1.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       apples1.setVisibility(ImageView.VISIBLE); 
      } 
     }, 9000); 

     Handler handler2 = new Handler(); 
     handler2.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       plus1.setVisibility(TextView.VISIBLE); 
      } 
     }, 9250); 
} 
public void stopLesson(View V){ 

} 
+2

你爲什麼要使用多個處理程序?爲什麼不是一個處理程序? – CommonsWare 2013-03-05 17:09:00

回答

12

而不是你必須用一個名稱來定義它的一個匿名的方式編寫可運行的任務,這樣以後你將有一個鏈接刪除:

//there is no need for multiple handlers 
//handler must be declared outside all functions, in order for you to use it everywhere. 

Handler handler = new Handler(); 
Runnable myFirstTask = new Runnable(){ 
     @Override 
       public void run() { 
        plate1.setVisibility(ImageView.VISIBLE); 
        plate2.setVisibility(ImageView.VISIBLE); 
        plate3.setVisibility(ImageView.VISIBLE); 
       } }; 

    Runnable mySecondTask = new Runnable(){ 
     @Override 
       public void run() { 
        plus1.setVisibility(TextView.VISIBLE); 
       } 

    }; 

    Runnable myThirdTask = new Runnable(){ 
     @Override 
       public void run() { 
        apples1.setVisibility(ImageView.VISIBLE); 
       } } 

    //you can put different tasks on the same handler object 

    while (!lessonIsRunning) { 
      handler.postDelayed(myFirstTask,6000); 
      handler.postDelayed(mySecondTask,9250); 
      handler.postDelayed(myThirdTask,9000); 
} 
public void stopLesson(View V){ 
    //notice that you don't need these, because the handlers are not recursive 
    //you don't have lines "handler.postDelayed(sameTask,someTime);" 
    //in your run Method of the runnable 
if(handler!=null){ 
    handler.removeCallbacks(myFirstTask); 
    handler.removeCallbacks(mySecondTask); 
    handler.removeCallbacks(myThirdTask); 
    //if this method is inside onPause or onDestroy add this line as well: 
    handler=null; 

} 
} 
+0

你嘗試過removeCallbacksAndMessages(null)方法,而不是removeCallback(runnable)的三行嗎?正如文件中所說,我對此表示滿意,我只是想知道它是否有效。 – 2013-03-05 17:39:37

+0

可能有其他回調,你不想刪除 – 2013-03-05 18:18:06

+0

我知道,但在這種具體情況?作品?因爲@RBZ有問題。 – 2013-03-05 19:17:25

4

你可以給

handler0.removeCallbacksAndMessages(null); 
handler1.removeCallbacksAndMessages(null); 
handler2.removeCallbacksAndMessages(null); 

一試。該文檔說,當您提交空令牌時,所有回調和消息都將被刪除。

+0

感謝您的快速回答!問題是我得到handler1.removeCallbacksAndMessages(null)上的nullPointerException; 當我在handler0正在播放時停止它。有沒有一種方法來檢查處理程序的狀態?或者你可以考慮的任何布爾條件? – RBZ 2013-03-05 16:46:17

+0

該問題與removeCallbacksAndMessages參數無關。可能handler1爲null。 – Blackbelt 2013-03-05 16:54:29

+1

@RBZ:此外,只能從處理程序的主線程(例如,主應用程序線程)調用這些方法。 – CommonsWare 2013-03-05 17:07:58

相關問題