无法 stop/update 处理程序(removeCallbacksAndMessages)
Unable to stop/update handler (removeCallbacksAndMessages)
我已经实施了一项服务,该服务以自定义间隔周期性地更新 UI。
更改更新间隔时,我想停止当前处理程序并以新间隔重新开始。
正如我之前在其他线程中读到的那样,在处理程序被触发后不可能中断它。
但是提到的解决方案,比如删除回调和消息
handler.removeCallbacksAndMessages(null);
handler.removeCallbacks(runnable);
例如实施终止开关
started = false;
if(started) {
//do work..
}
根本没有帮助。当我以更新的间隔重新启动我的服务时,处理程序不会停止,导致多个处理程序同时 运行。
有人知道我做错了什么或如何改进吗?
感谢任何帮助!谢谢!
//Mainactivity
startService(new Intent(this, updateService.class));
public class updateService extends IntentService {
private boolean started = false;
private Handler handler = new Handler();
public updateService() {
super("updateService");
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
// Try to stop handler when startservice is called and start new one afterwards
stop();
start();
return super.onStartCommand(intent, flags, startId);
}
public void start() {
started = true; // Didn't help
int timer = config_store.getPreference("refresh_interval"));
if (timer !=0){
handler.postDelayed(runnable, timer);
}
else {
stop();
}
}
public void stop() {
started = false;
handler.removeCallbacksAndMessages(null); // Didn't help
handler.removeCallbacks(runnable); // Didn't help
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
if(started) { // Didn't help.
// Update job
doJob();
// Start over again
start();
}
}
};
}
好的,我自己修好了:
延迟处理程序不是循环调用方法的最佳解决方案。
现在使用 TimerTask:
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
if(mTimer != null) {
mTimer.cancel();
}
mTimer = new Timer();
int timer = Integer.parseInt(getPreference("refresh_interval")) * 1000;
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), timer, timer);
return super.onStartCommand(intent, flags, startId);
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
// Do job
}
});
}
}
我已经实施了一项服务,该服务以自定义间隔周期性地更新 UI。 更改更新间隔时,我想停止当前处理程序并以新间隔重新开始。 正如我之前在其他线程中读到的那样,在处理程序被触发后不可能中断它。 但是提到的解决方案,比如删除回调和消息
handler.removeCallbacksAndMessages(null);
handler.removeCallbacks(runnable);
例如实施终止开关
started = false;
if(started) {
//do work..
}
根本没有帮助。当我以更新的间隔重新启动我的服务时,处理程序不会停止,导致多个处理程序同时 运行。
有人知道我做错了什么或如何改进吗? 感谢任何帮助!谢谢!
//Mainactivity
startService(new Intent(this, updateService.class));
public class updateService extends IntentService {
private boolean started = false;
private Handler handler = new Handler();
public updateService() {
super("updateService");
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
// Try to stop handler when startservice is called and start new one afterwards
stop();
start();
return super.onStartCommand(intent, flags, startId);
}
public void start() {
started = true; // Didn't help
int timer = config_store.getPreference("refresh_interval"));
if (timer !=0){
handler.postDelayed(runnable, timer);
}
else {
stop();
}
}
public void stop() {
started = false;
handler.removeCallbacksAndMessages(null); // Didn't help
handler.removeCallbacks(runnable); // Didn't help
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
if(started) { // Didn't help.
// Update job
doJob();
// Start over again
start();
}
}
};
}
好的,我自己修好了: 延迟处理程序不是循环调用方法的最佳解决方案。 现在使用 TimerTask:
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
if(mTimer != null) {
mTimer.cancel();
}
mTimer = new Timer();
int timer = Integer.parseInt(getPreference("refresh_interval")) * 1000;
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), timer, timer);
return super.onStartCommand(intent, flags, startId);
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
// Do job
}
});
}
}