Check if Android handler has callbacks

deucalion0 picture deucalion0 · Apr 4, 2013 · Viewed 10k times · Source

I have some code which sets a timer, but if a user sets the timer while it is in use I need to remove the runnable which runs the timer and start it again. But when no handler callback runnable exists and this code is called it crashes my application. So I need to check if a handler is running, if so then end it and restart it, but looking through the documentation and other Stackoverflow questions, I cannot see if this is possible.

Here is my code, I have commented around the code which should only be executed if a handler runnable exists:

    submitTimer.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            String s = timer.getText().toString();
            if(!s.equals(""))
            {
//I NEED TO CHECK A RUNNABLE HANDLER EXISTS AND IF SO THEN RUN THIS CODE, IF NOT IGNORE THIS CODE
            Map.handler.removeCallbacks(Map.getRunnable());
            Map.runnable.run();
//I NEED TO CHECK A RUNNABLE HANDLER EXISTS AND IF SO THEN RUN THIS CODE, IF NOT IGNORE THIS CODE
            int l = Integer.parseInt(s);
            Map.timerinmins = l;
            timer.setHint("Current Timer is "+Map.timerinmins);
            timer.setText("");
            Toast.makeText(Preferences.this, "Timer is set!", Toast.LENGTH_SHORT).show();
            }

            else
            {

                Toast.makeText(Preferences.this, "No value was entered", Toast.LENGTH_SHORT).show();
            }
    }

});

Can anyone help me figure out a way of checking the handlers current state?

Answer

Jay Snayder picture Jay Snayder · Apr 4, 2013

If you want you could send an empty message when you first put a callback in and then check for that message in the handler. This empty message could represent that there is a callback present. Removing that message later could then be used similarly to see if the callback is still there. Don't have a relevant situation such as this to go from, but thought that I would at least try and share a possibility.

...
Map.handler.sendEmptyMessage(CALLBACK_PRESENT_INTEGER);
...
if(Map.handler.hasMessages(CALLBACK_PRESENT_INTEGER)
...
Map.handler.removeMessage(CALLBACK_PRESENT_INTEGER);
...

This is probably not ideal, but could be a potential solution if you have access to your handler from the point where your callback is used. Not sure if there is a direct way to find out.