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

问题描述 投票:0回答:1

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

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

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

android progress-bar
1个回答
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);
                }
            });
        }
    }
});

}

© www.soinside.com 2019 - 2024. All rights reserved.