Skip to content Skip to sidebar Skip to footer

Android : How To Pause And Resume Runnable Thread?

I'm using a postDelayed runnable thread, I need to pause and resume that thread when I press a button. Please anyone help me on that. This is my thread: protected void animatio

Solution 1:

I apologize for the poor formatting

boolean isPaused = true;

 playButton.setOnClickListener(newOnClickListener() { 
     publicvoidonClick(View v) { 
        isPaused = !isPaused;
        if(isPaused) { 
            music4.removeCallbacks(runnable4);
        }
        else { 
            music4.postDelayed(runnable4, 100);
        }
     }
});

Solution 2:

I have a similar issue that I am confused about. As taught here I'm using postDelayed() to have a Runnable start after a delay, let´s say after 4 seconds. I want to implement a pause function so that if I pause, and at that point in time there for example is 2 seconds left until the runnable code was supposed to start, then when I later resume I want the delay to resume at the exact point in time where it became paused, so that the postDelayed operation will continue and will have the thread run 2 seconds after I resume. A kind of timer freeze()/unfreeze() operation. Will I need to use timers to accomplish that ?

Post a Comment for "Android : How To Pause And Resume Runnable Thread?"