2014-10-30 158 views
-2

我有Runnable並有一些音樂播放後按鈕背景顏色變化處理程序。 現在,我可以如何停止使用runnable播放的音樂,在按鈕單擊命名停止時延遲700毫秒。停止運行按鈕點擊

 handler=new Handler(); 
    runnable = new Runnable() { 
    @Override 
    public void run() { 

    for (int i = 0; i<Uirkeys1.length; i++) { 
     final int value = i; 
     try { 

       Thread.sleep(700); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
       handler.post(new Runnable() { 
      @Override 
      public void run() { 

     copyView.get(cnt1).getBackground().setAlpha(100); 
     ModeSound(Integer.parseInt(CopyMultimap.get(copykey[cnt1]).get(3)));       
       } 
       }); 
       try { 
      Thread.sleep(800); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
       handler.post(new Runnable() { 
       @Override 
        public void run() 
        copyView.get(cnt1).getBackground().setAlpha(500);    
        cnt1++; 
         } 
         }); 
          } 
         } 
          }; 
       new Thread(runnable).start(); 
       cnt1=0; 
        } 
       }); 
} 
+0

可以設置布爾值,並根據條件停止你的任務orelse使用removecallbacks – micky 2014-10-30 08:03:32

回答

1

你可以創建一個類MyRunnable實現Runnable接口,並在它

 private boolean stopped = false; 
    public void stop() { stopped = true; } 

創建一個字段,那麼你應該在你的執行循環

 if(!stopped) {..do work..} else return . 

檢查,你可以的課程在您的MyRunnable對象上調用stop()方法。

+0

謝謝你明白了.. – prasad 2014-10-30 08:42:10