Android:如何停止Runnable?

时间:2012-02-26 22:58:47

标签: android runnable

我试过这种方式:

private Runnable changeColor = new Runnable() {
   private boolean killMe=false;
   public void run() {
       //some work
       if(!killMe) color_changer.postDelayed(changeColor, 150);
   }
   public void kill(){
       killMe=true;
   }
};

但我无法访问kill()方法!

5 个答案:

答案 0 :(得分:45)

使用SDK提供的现有API,实现您自己的thread.kill()机制。在threadpool内管理您的线程创建,并使用Future.cancel()来终止正在运行的线程:

ExecutorService executorService = Executors.newSingleThreadExecutor();
Runnable longRunningTask = new Runnable();

// submit task to threadpool:
Future longRunningTaskFuture = executorService.submit(longRunningTask);

... ...
// At some point in the future, if you want to kill the task:
longRunningTaskFuture.cancel(true);
... ...

取消方法将根据您的任务运行状态而有所不同,请查看API以获取更多详细信息。

答案 1 :(得分:15)

public abstract class StoppableRunnable implements Runnable {

    private volatile boolean mIsStopped = false;

    public abstract void stoppableRun();

    public void run() {
        setStopped(false);
        while(!mIsStopped) {
            stoppableRun();
            stop();
        }
    }

    public boolean isStopped() {
        return mIsStopped;
    }

    private void setStopped(boolean isStop) {    
        if (mIsStopped != isStop)
            mIsStopped = isStop;
    }

    public void stop() {
        setStopped(true);
    }
}

班......

    private Handler mHandler = new Handler();

public void onStopThread() {
    mTask.stop();       
    mHandler.removeCallbacks(mTask);
}

public void onStartThread(long delayMillis) {
    mHandler.postDelayed(mTask, delayMillis);
}

private StoppableRunnable mTask = new StoppableRunnable() {
    public void stoppableRun() {        
                    .....
            onStartThread(1000);                
        }
    }
};

答案 2 :(得分:11)

mHandler.removeCallbacks(updateThread);

答案 3 :(得分:3)

changeColor被声明为Runnable,没有kill()方法。

您需要创建自己的扩展Runnable的界面并添加(公共)kill()方法。

答案 4 :(得分:0)

您可以使用这种方式

停止可运行线程 // ================================================ =============================

 ThreadUtil.startTask(() -> {
                        // doTask
                    }, 1500);


//==============================================================================
 public class ThreadUtil {

        private static Handler handler;
        private static Runnable runnable;

        public static void startTask(IThreadTask iThreadTask, long delayTime) {
            stopTask();
            handler = new Handler();
            runnable = () -> {
                iThreadTask.doTask();
            };

            if (handler == null || runnable == null) {
                return;
            }

            handler.postDelayed(runnable, delayTime);

        }

        public static void stopTask() {
            try {
                handler.removeCallbacks(runnable);
                handler.removeCallbacksAndMessages(null);
                handler = null;
                runnable = null;

            }catch (Exception e){
                Log.e("ThreadUtil:","Error:"+e.toString());

            }

        }


        public interface IThreadTask {
            void doTask();
        }


    }