如何在一分钟内显示进度条?

5

我在安卓中有一个水平进度条。我需要让它在60秒内完成。

为什么下面的代码不起作用?

int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
     progressHandler.postDelayed((new Runnable() {
        public void run() {                     
           progressBarHorizontal.setProgress(progress);                    
        }                   
     }),progress * 1000);
     progress++;
}

请建议其他方法。我尝试了这个:
new Thread(new Runnable() {
                int progress = 0;       
                public void run() {
                    long timerEnd = System.currentTimeMillis() + 60 * 1000;

                    while (timerEnd >  System.currentTimeMillis() ) {

                        progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
                        // Update the progress bar
                        progressHandler.post(new Runnable() {
                            public void run() {
                                progressBarHorizontal.setProgress(progress);
                            }
                        });                         
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.w("tag","Progress thread cannot sleep");
                        }
                    }
                }
            }).start(); 

但它也不起作用。

第二段代码实际上是有效的,问题出在我的逻辑上。


你可以使用AsynTask吗? - Sam
4个回答

7

你可以尝试使用 CountDownTimer

pd = ProgressDialog.show(MovementEntry.this, "", "Please Wait...",
                true, false);
pd.show();
new CountDownTimer(60000, 1000) {
  @Override
  public void onTick(long millisUntilFinished) {
      //this will be done every 1000 milliseconds ( 1 seconds )
      int progress = (60000 - millisUntilFinished) / 1000;
      pd.setProgress(progress);
  }

   @Override
   public void onFinish() {
      //the progressBar will be invisible after 60 000 miliseconds ( 1 minute)
      pd.dismiss();
   }

}.start();

CountDownTimer是一个不错的方法,但ProgressDialog会隐藏我的状态更新。我会尝试使用CountDownTimer,谢谢。 - Arun Babu
在自定义对话框中,使用我在问题中提供的第二段代码,我已经让它正常工作了。感谢您的帮助.. :) - Arun Babu
很好,所以请接受你认为对你有帮助的答案。 - Houcine

3
这个会在状态栏更新,直到它达到最大值:
private int progress = 0;
private final int pBarMax = 60;

...

final ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);     
pBar.setMax(pBarMax);
final Thread pBarThread = new Thread() {
    @Override
    public void run() {
        try {
            while(progress<=pBarMax) {
                pBar.setProgress(progress);
                sleep(1000);
                ++progress; 
            }
        }
        catch(InterruptedException e) {
        }
    }
};

pBarThread.start();

当我运行这个程序时,至少没有出现任何异常。 - vilpe89

2
您可以尝试使用以下代码实现此功能:
 Thread timer = new Thread(){
public void run(){
    try{
        sleep(10000);
        while(progressBarStatus < 10000){
            StartPoint.this.runOnUIThread(new Runnable(){
                public void run()
                {
                    progressBar.setProgress(progressBarStatus);
                    progressBarStatus += 1000;
                }
            });

        }
    }catch(InterruptedException e){
        e.printStackTrace();
    }finally{

    }
}
};
timer.start();

我的进度条在一个对话框中,同时我还有状态更新内容,所以显示进度条会隐藏更新。 - Arun Babu
1
Thread.sleep()会阻塞它10秒钟,但在显示进度对话框时不会计算10秒钟。 - Houcine
是的,我尝试过了,我需要更新我的进度条,不想显示一个新的对话框。 - Arun Babu
使用我在问题中提供的第二段代码,我已经让它正常工作了。感谢您的帮助.. :) - Arun Babu

2
这段代码对我来说可行。
new Thread(new Runnable() {

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

                    while (timerEnd >  System.currentTimeMillis()) {

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

                        progressHandler.post(new Runnable() {
                            public void run() {                     
                                    progressBarHorizontal.setProgress(progress);                    
                            }                   
                        });

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.w("App","Progress thread cannot sleep");
                        }
                    }
                    progressHandler.post(new Runnable() {
                        public void run() {                     
                                okButton.performClick();                    
                        }                   
                    });
                }
            }).start(); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接