Skip to content Skip to sidebar Skip to footer

Android, Pausing And Resuming Handler Callbacks

I have a handler that I am using as follows: handler.postDelayed(Play, 1000); when my application onPause() is called before this is done, I need to pause it and tell it not to pe

Solution 1:

You need to subclass Handler and implement pause/resume methods as follows (then just call handler.pause() when you want to pause message handling, and call handler.resume() when you want to restart it):

classMyHandlerextendsHandler {
    Stack<Message> s = newStack<Message>();
    booleanis_paused=false;

    publicsynchronizedvoidpause() {
        is_paused = true;
    }

    publicsynchronizedvoidresume() {
        is_paused = false;
        while (!s.empty()) {
            sendMessageAtFrontOfQueue(s.pop());
        }
    }

    @OverridepublicvoidhandleMessage(Message msg) {
        if (is_paused) {
            s.push(Message.obtain(msg));
            return;
        }else{
               super.handleMessage(msg);
               // otherwise handle message as normal// ...
        }
    }
    //...
}

Solution 2:

Have you tried with:

@OverridepublicvoidonPause()
{
  handler.removeCallbacks(Play);
  Soundmanager.autoPause()
}

Ger

Solution 3:

Modifying the answer given by CpcCrunch. There handleMessage not worked for me, so instead of it using dispatchMessage. Note: Below code is written in Kotlin:

classCustomHandler: Handler() {

    var s = Stack<Message>()
    var is_paused = false@Synchronizedfunpause() {
        is_paused = true
    }

    @Synchronizedfunresume() {
        is_paused = falsewhile (!s.empty()) {
            sendMessageAtFrontOfQueue(s.pop())
        }
    }

    overridefundispatchMessage(msg: Message?) {
        if (is_paused) {
            s.push(Message.obtain(msg))
            return
        } else {
            super.dispatchMessage(msg)
        }
    }
}

Solution 4:

publicclassYourActivityextendsAppCompatActivity {

    privatestaticboolean handlerflag=false;
    privateHandler handler;
    privateRunnable runnable;
    private int myind=0,index=0,count=0;

    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activtiy);         
        //oncreate exe only
        handlerflag=true;
        handler = newHandler();
        startyourtime(0);
 }
  privatevoidstartyourtime(int a) {

    myind=0;
    for (index=a; index<10 ;index++) {
            myind++;
            runnable=newRunnable() {
                count++;
                @Overridepublicvoidrun() {
                          //your code here
               }
            };handler.postDelayed(runnable, Constants.TIME_LIMIT * myind);

   }
    @OverrideprotectedvoidonPause() {
        super.onPause();
        handlerflag=false;
        handler.removeCallbacksAndMessages(null);
    }
    @OverrideprotectedvoidonResume() {
        super.onResume();
        if(!handlerflag)
        {
           startyourtime(count);

        }
    }
}

Solution 5:

I came up with an alternative to CpnCrunch when wanting to pause/resume Runnables in a queue. To have methods that has been called whilst still connecting and is offline, once online, resume the queue and all runnables are executed.

Instead of using Handler, use ExecutorService:

publicclassExecutorQueueServiceextendsThreadPoolExecutor {
    private Stack<Runnable> runnables = newStack<>();
    privatebooleanpaused=false;

    publicExecutorQueueService() {
        super(1, 1, 0L, TimeUnit.MILLISECONDS, newLinkedBlockingQueue<Runnable>());
    }

    publicsynchronizedvoidpause() {
        paused = true;
    }

    publicsynchronizedvoidresume() {
        paused = false;
        while (!runnables.empty()) {
            execute(runnables.pop());
        }
    }

    publicsynchronizedbooleanisPaused() {
        return paused;
    }

    @Overridepublicvoidexecute(Runnable runnable) {
        if (paused) {
            runnables.push(runnable);
        } else {
            super.execute(runnable);
        }
    }
}

Using it is similar to Handler, but instead of post(runnable), use execute(runnable)

Post a Comment for "Android, Pausing And Resuming Handler Callbacks"