Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Android handler has callbacks

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?

like image 414
deucalion0 Avatar asked Apr 04 '13 16:04

deucalion0


People also ask

What is Handler callback?

android.os.Handler.Callback. Callback interface you can use when instantiating a Handler to avoid having to implement your own subclass of Handler.

What is Handler PostDelayed in Android?

PostDelayed(IRunnable, Int64) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. PostDelayed(Action, Int64) PostDelayed(IRunnable, Object, Int64) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.

How do I cancel handler?

removecallback and handler = null; to cancel out the handle just to keep the code clean and make sure everything will be removed.


1 Answers

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.

like image 126
Jay Snayder Avatar answered Oct 23 '22 05:10

Jay Snayder