英文:
Stopping nested delayed callback Runnable Java
问题
我已经设置了一个回调,每一秒运行一次,但似乎无法停止这个进程。我在4个不同的论坛上提过这个问题,似乎没有人能回答这个问题,这让我非常沮丧。有没有办法停止这个进程?
方法如下:
void startCallbackTimer() {
callbackRunnable = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run() {
if (isIni && dateSent && UpdateSent) {
Result result = per.getResult();
Update(result);
}
callbackHandler.postDelayed(this, 1000);
}
};
callbackHandler.postDelayed(callbackRunnable, 1000);
}
我尝试多次调用以下代码:
callbackHandler.removeCallbacks(callbackRunnable);
但是没有成功,请问有没有人可以回答这个问题,我被困住了。
英文:
I have set up a callback to run every one second, I cannot seem to stop the process. I have asked this question on 4 different forums and no one can seem to answer this question it is very frustrating. Is there a way to stop this?
The method
void startCallbackTimer() {
callbackRunnable = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run() {
if (isIni&&dateSent&&UpdateSent) {
Result result = per.getResult();
Update(result);
}
callbackHandler.postDelayed(this, 1000);
}
};
callbackHandler.postDelayed(callbackRunnable, 1000);
}
I have tried to call multiple times:
callbackHandler.removeCallbacks(callbackRunnable);
But no luck, please can anyone answer this question i am stuck.
答案1
得分: 0
你应该检查是否已经有答案...
你需要将 null 传递给参数
callbackHandler.removeCallbacks(null)
如果这不起作用,你可以使用
callbackHandler.removeCallbacksAndMessages(callbackRunnable)
英文:
You should check that this is already answered...
You need to pass null to the parameter
callbackHandler.removeCallbacks(null)
If that does not work you can use
callbackHandler.removeCallbacksAndMessages(callbackRunnable)
专注分享java语言的经验与见解,让所有开发者获益!
评论