Android暂停和恢复后台活动

时间:2016-05-19 13:50:41

标签: android multithreading handler

在我的应用的一项活动中,有一个每3秒钟更换一次的imagview和textview。

我首先使用处理程序和线程runnable进行设置。它运行正常,但是当我使用带有暂停按钮的removeCallbacks时,它不会在Thread的中间停顿。它在暂停之前完成Thread runnable。我需要它在点击按钮时暂停,无论它在runnable中的哪个位置。

我考虑过使用asynctask,但我不确定这是如何工作的。

我知道必须有办法做到这一点,因为你几乎可以暂停你下载的任何游戏。有没有人对最佳/最简单的方法有任何建议?

非常感谢!! :)

public class Workout extends AppCompatActivity {

private String[] messages = {"1", "2", "3", "4", "5"};
final Handler handler = new Handler();
private int nextIndex;
String mDrawableName[] = {"bodypart70", "equipment70", "settings70", "time70", "bodypart70"};
private int nextImage;
ImageView image;
private ProgressBar progressBar;
private int progressStatus = 0;
Runnable r = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout);

    image = (ImageView) findViewById(R.id.exerciseImage);
    final TextView text = (TextView)findViewById(R.id.textView4);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);


    //changing
    r = new Runnable(){
        @Override
        public void run(){
            if (messages.length > nextIndex) {

                //change textview
                text.setText(messages[nextIndex++]);
                //change image
                int resID = getResources().getIdentifier(mDrawableName[nextImage++] , "drawable", getPackageName());
                image.setImageResource(resID);
                //restart progress bar
                progressStatus = 0;
                progress();
                //do it after 3 seconds
                handler.postDelayed(this, 3000);
            }
        }
    };

    handler.postDelayed(r, 0);


}



public void progress(){

    new Thread(new Runnable() {

        public void run() {
            long timerEnd = System.currentTimeMillis() + 3 * 1000;

            while (timerEnd >  System.currentTimeMillis()) {

                progressStatus = 3 - (int) (timerEnd - System.currentTimeMillis()) / 1000;
                // Update the progress bar

                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                    }
                });

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    Log.w("App", "Progress thread cannot sleep");
                }
            }
        }
    }).start();

}


public void pauseWorkout(View view){
    handler.removeCallbacks(r);

}

public void resumeWorkout(View view){
    handler.postDelayed(r, 0);          
}

}

1 个答案:

答案 0 :(得分:1)

按下暂停按钮后,你的progress()函数中的while循环看起来仍然有效。

尝试以下操作:创建一个全局布尔值,指示您处于暂停或恢复状态。把它放在代码的顶部:

boolean resume = true;

更新正在进行的条件()

while (resume && timerEnd >  System.currentTimeMillis()) {

更新锻炼功能:

public void pauseWorkout(View view){
    handler.removeCallbacks(r);
    resume = false;
}

public void resumeWorkout(View view){
    handler.postDelayed(r, 0); 
    resume = true;         
}