2014-10-30 123 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