如何在android中暂停并恢复进度条?

如何在android中暂停并恢复进度条?,android,progress-bar,Android,Progress Bar,嗨,我正在使用进度条,我需要暂停并恢复它。这是我的代码: new Thread(new Runnable() { public void run() { while (progressStatus < 100) { progressStatus += 1; handler.post(new Runnable() { public void run

嗨,我正在使用进度条,我需要暂停并恢复它。这是我的代码:

new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 1;

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

                    }
                });
                try {

                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

任何人都可以帮助我。提前谢谢。

这是我使用水平进度条的代码

public class currentsong extends AppCompatActivity {

ImageView img_play, shuffle, repeat, song_image;
boolean state_playbutton, state_shuffle, state_repeat;
TextView name_of_song, name_of_artist;
1如下所示声明这些变量:-

ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();
boolean temp=true;

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


    img_play = findViewById(R.id.play);
    shuffle = findViewById(R.id.shuffle);
    repeat = findViewById(R.id.repeat);
    song_image = findViewById(R.id.song_image);
    name_of_song = findViewById(R.id.name_of_song);
    name_of_artist = findViewById(R.id.name_of_artist);
    progressBar = findViewById(R.id.progressBar);

    state_playbutton = true;
    state_shuffle = true;
    state_repeat = true;
2当我的活动开始时,我的线程应该运行,所以我使用了thread.start

    thread.start();

    img_play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(state_playbutton)
            {
                img_play.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                state_playbutton = false;
3I使用此变量,以便while循环不会在下面定义的线程中运行,即线程执行将暂停

                temp = false;
            }
            else
            {
                img_play.setImageResource(R.drawable.ic_pause_black_24dp);
                state_playbutton = true;
4现在要恢复线程执行,请设置temp=true,我使用了thread.start,这样线程将恢复到原来的位置

                temp = true;
                thread.start();
            }

        }
    });


}

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        while (progressStatus < 300 && temp) {
            // Update the progress status
            progressStatus += 1;

            // Try to sleep the thread for 100 milliseconds
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // Update the progress bar
            handler.post(new Runnable() {
                @Override
                public void run() {
                    progressBar.setProgress(progressStatus);
                }
            });
        }
    }
});

}

您不会暂停或恢复进度条。您可以通过setProgressint方法设置进度。暂停或继续更新进度条的操作。在您的情况下,您可以创建一个变量并在progressbar.setProgress之前检查它;如果要暂停该条,可以将该变量设置为false。