我怎样才能在android中暂停和恢复进度条?

时间:2014-04-30 05:52:57

标签: android progress-bar

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

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();

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

1 个答案:

答案 0 :(得分:0)

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

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;

3)我使用了此变量,以便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);
                }
            });
        }
    }
});

}