如何停止新的Thread(new Runnable()).run
我是Android开发的新手:D,我需要你的帮助:)
我有这个代码,我想在执行
后停止它 new Thread(new Runnable() {
@Override public void run() {
Log.e("MotionLogger", "SU");
Log.i(TAG, "click " + Thread.currentThread().toString());
try {
finalCommandLine.writeBytes(command.toString() + '\n');
finalCommandLine.writeBytes("exit\n");
finalCommandLine.flush();
finalCommandLine.close();
finalSuShell.waitFor();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}).start();
提前致谢:)
答案 0 :(得分:1)
在您的情况下,您可以使用thread.interrupt
如果你想在UI线程上运行,可以像这样创建处理程序
Handler handler= new Handler();
然后像这样创建Runnable
Runnable runnable= new Runnable() {
public void run() {
//do your work here
}
};
//start like this
runnable.run();
//stop like this
handler.removeCallbacks(runnable);