title: Pausing and resuming a runnable thread
body: <p>I have an activity (not my main activity) that plays some sounds and draw some graphics in a runnable thread. There is an imagebutton with which I'm supposed to suspend and resume the thread. But whatever I try the thread just runs on. I'm totally stuck! Please help.</p>
  
  <pre><code>public class PlayInterval extends Activity {
     private Handler customHandler;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playinterval);
        final ImageButton playPauseButton = (ImageButton) findViewById(R.id.playPauseButton);//Play/Pause
        playPauseButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
             onPlayPauseClicked(playPauseButton,(String)playPauseButton.getTag());
          }
       });
       customHandler = new Handler();
       new Thread(new Task()).start();
    }
    @Override
    public void onBackPressed() {
       customHandler.removeCallbacksAndMessages(null);
       finish();
    }
    class Task implements Runnable {
       @Override
       public void run() {
          // Do really cool stuff
          // and even cooler stuff
          customHandler.postDelayed(this, 100);
       }
    }
    private void onPlayPauseClicked(ImageButton playPauseButton, String status) {
       if (status == "playing") {
          playPauseButton.setTag("paused");
          customHandler.removeCallbacksAndMessages(null); //TODO suspend here not working!
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
             playPauseButton.setImageResource(getResources().getIdentifier("pause_image", "drawable", getPackageName()));
          } else {
              playPauseButton.setImageDrawable(getDrawable(getResources().getIdentifier("pause_image", "drawable", getPackageName())));
          }
      } else {
          playPauseButton.setTag("playing");
          customHandler.postDelayed(this, 100); //TODO resume here not working!
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
              playPauseButton.setImageResource(getResources().getIdentifier("play_image", "drawable", getPackageName()));
          } else {
              playPauseButton.setImageDrawable(getDrawable(getResources().getIdentifier("play_image", "drawable", getPackageName())));
          }
      }
    }
  }
  </code></pre>