停止处理程序执行
要停止执行 Handler,请使用在其中运行的 runnable 删除附加到它的回调:
Runnable my_runnable = new Runnable() {
@Override
public void run() {
// your code here
}
};
public Handler handler = new Handler(); // use 'new Handler(Looper.getMainLooper());' if you want this handler to control something in the UI
// to start the handler
public void start() {
handler.postDelayed(my_runnable, 10000);
}
// to stop the handler
public void stop() {
handler.removeCallbacks(my_runnable);
}
// to reset the handler
public void restart() {
handler.removeCallbacks(my_runnable);
handler.postDelayed(my_runnable, 10000);
}