停止嵌套的延迟回调 Runnable Java

huangapple 未分类评论43阅读模式
英文:

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) 

huangapple
  • 本文由 发表于 2020年7月24日 14:55:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/63068371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定