暂停和恢复可运行的线程

时间:2015-09-07 18:41:11

标签: java android multithreading

我有一个活动(不是我的主要活动)播放一些声音并在可运行的线程中绘制一些图形。有一个图像按钮,我应该暂停和恢复该线程。但无论我尝试什么线程都运行。我完全卡住了!请帮忙。

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 >= 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 >= Build.VERSION_CODES.JELLY_BEAN) {
            playPauseButton.setImageResource(getResources().getIdentifier("play_image", "drawable", getPackageName()));
        } else {
            playPauseButton.setImageDrawable(getDrawable(getResources().getIdentifier("play_image", "drawable", getPackageName())));
        }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我从来没有得到答案,所以经过反复试验后,我想出了这个。我在StackOverflow上有很多红色,我理解这是 NOT 它应该完成的方式。但它工作得非常好,所以如果有人能够解释为什么我们不应该使用这样的同步方法并提供更好的答案,我会感激不尽。 干杯

public class PlayInterval extends Activity {
   private Handler customHandler;
   Boolean isRunning;
   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);
         }
      });
      customHandler = new Handler();
      isRunning=true;
      new Thread(new Task()).start();
   }

   @Override
   public void onBackPressed() {
      customHandler.removeCallbacksAndMessages(null);
      finish();
   }

   class Task implements Runnable {
      @Override
      public void run() {
      if (isRunning == true){
         // Do really cool stuff
         // and even cooler stuff
      } else {
         // it is paused
      }
      customHandler.postDelayed(this, 100);
   }


   private syncronized void onPlayPauseClicked(ImageButton playPauseButton) {
     if (isRunning==true) {
        isRunning=false;
        playPauseButton.setImageResource(getResources().getIdentifier("play_image", "drawable", getPackageName()));
     } else {
        isRunning=true;
        playPauseButton.setImageResource(getResources().getIdentifier("pause_image", "drawable", getPackageName()));
      }
  }
}