How to cancel a handler before time in Android code?

喜欢而已 提交于 2019-11-30 17:25:29

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);

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.

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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!