androidrunnable使用,Android的:如何停止的Runnable?
I tried this way:
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;
}
};
but I can't access kill() method!
解決方案
Instead implement your own thread.kill() mechanism, using existing API provided by the SDK. Manage your thread creation within a threadpool, and use Future.cancel() to kill the running thread:
ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor();
Runnable longRunningTask = new Runnable();
// submit task to threadpool:
Future longRunningTaskFuture = threadPoolExecutor.submit(longRunningTask);
... ...
// At some point in the future, if you want to kill the task:
longRunningTaskFuture.cancel(true);
... ...
Cancel method will behave differently based on your task running state, check the API for more details.
總結
以上是生活随笔為你收集整理的androidrunnable使用,Android的:如何停止的Runnable?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 得力助手 消防员的 消防机器人_消防机器
- 下一篇: inaflash什么意思中文_英文“fl