如何在 Android 代码中提前取消处理程序?

发布于 2024-11-04 18:13:12 字数 534 浏览 0 评论 0原文

我创建了 1 分钟延迟计时器来关闭服务(如果服务未完成)。看起来像这样:

private Handler timeoutHandler = new Handler();

在 onCreate() 内部

timeoutHandler.postDelayed(new Runnable()
        {
            public void run()
            {
                Log.d(LOG_TAG, "timeoutHandler:run");

                DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute");
                finalizeService();
            }
        }, 60 * 1000);

如果我在这 1 分钟之前完成工作 - 我想取消这个延迟的事情,但不知道如何取消。

I create 1 minute delayed timer to shutdown service if it's not completed. Looks like this:

private Handler timeoutHandler = new Handler();

inside onCreate()

timeoutHandler.postDelayed(new Runnable()
        {
            public void run()
            {
                Log.d(LOG_TAG, "timeoutHandler:run");

                DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute");
                finalizeService();
            }
        }, 60 * 1000);

If I get job accomplished before this 1 minute - I would like to get this delayed thing cancelled but not sure how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠北森屋 2024-11-11 18:13:12

您实际上无法使用匿名 Runnable 来做到这一点。将 Runnable 保存到命名变量怎么样?

Runnable finalizer = new Runnable()
    {
        public void run()
        {
            Log.d(LOG_TAG, "timeoutHandler:run");

            DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute");
            finalizeService();
        }
    };
timeoutHandler.postDelayed(finalizer, 60 * 1000);

...

// Cancel the runnable
timeoutHandler.removeCallbacks(finalizer);

You can't really do it with an anonymous Runnable. How about saving the Runnable to a named variable?

Runnable finalizer = new Runnable()
    {
        public void run()
        {
            Log.d(LOG_TAG, "timeoutHandler:run");

            DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute");
            finalizeService();
        }
    };
timeoutHandler.postDelayed(finalizer, 60 * 1000);

...

// Cancel the runnable
timeoutHandler.removeCallbacks(finalizer);
故人如初 2024-11-11 18:13:12

如果您不想保留可运行的引用,您可以简单地调用:

timeoutHandler.removeCallbacksAndMessages(null);

官方文档说:

...如果 token 为 null,则所有回调和消息都将被删除。

If you don't want to keep a reference of the runnable, you could simply call:

timeoutHandler.removeCallbacksAndMessages(null);

The official documentation says:

... If token is null, all callbacks and messages will be removed.

梦屿孤独相伴 2024-11-11 18:13:12

您可能希望将 postDelayed 的使用替换为 sendMessageDelayed 的使用,如下所示:

private Handler timeoutHandler = new Handler(){
    @Override
    public void handleMessage(Message msg)
    {
            switch (msg.what){
        case 1:
            ((Runnable)msg.obj).run();
            break;
        }
    }
};

然后发布消息:

Message m = Message.obtain();
m.what = 1;
m.obj = new Runnable(){
            public void run()
            {
                Log.d(LOG_TAG, "timeoutHandler:run");

                DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute");
                finalizeService();
            }
        };
timeoutHandler.sendMessageDelayed(m, 60 * 1000);

然后取消:

timeoutHandler.removeMessages(1);

无需跟踪可运行程序。

You might want to replace use of postDelayed with use of sendMessageDelayed like so:

private Handler timeoutHandler = new Handler(){
    @Override
    public void handleMessage(Message msg)
    {
            switch (msg.what){
        case 1:
            ((Runnable)msg.obj).run();
            break;
        }
    }
};

Then post a Message:

Message m = Message.obtain();
m.what = 1;
m.obj = new Runnable(){
            public void run()
            {
                Log.d(LOG_TAG, "timeoutHandler:run");

                DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute");
                finalizeService();
            }
        };
timeoutHandler.sendMessageDelayed(m, 60 * 1000);

and then cancel:

timeoutHandler.removeMessages(1);

No tracking of the runnable necessary.

茶花眉 2024-11-11 18:13:12

如果我在这 1 分钟之前完成工作 - 我想取消这个延迟的事情,但不知道如何取消。

使用 Handler.removeCallbacks(yourRunnable )

If I get job accomplished before this 1 minute - I would like to get this delayed thing cancelled but not sure how.

Use Handler.removeCallbacks(yourRunnable).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文