23

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.

Swati Garg
  • 995
  • 1
  • 10
  • 21
katit
  • 17,375
  • 35
  • 128
  • 256

4 Answers4

48

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);
Brian Dupuis
  • 8,136
  • 3
  • 25
  • 29
  • 1
    tiemoutHandler.removeCallbacksAndMessages(null); to remove all callbacks to handle an anonymous Runnable. (as mentioned in AbdelHady answer) – Lorenz03Tx Jul 22 '16 at 13:45
  • 2
    That's dandy as long as you don't mind removing ALL callbacks, not just this one. OP asked about removing a particular callback. – Brian Dupuis Jul 26 '16 at 20:34
15

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.

AbdelHady
  • 9,334
  • 8
  • 56
  • 83
10

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.

Femi
  • 64,273
  • 8
  • 118
  • 148
2

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

Wroclai
  • 26,835
  • 7
  • 76
  • 67