如何使用线程更新进度条

发布于 2024-11-06 17:15:33 字数 66 浏览 2 评论 0原文

如何在android中使用后台线程更新进度条?它还会改变进度条中的进度。请帮忙。

Android时尚

How to update progress bar using Background thread in android? It would also change the progress in progress bar. Please help.

AndroidVogue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风吹过旳痕迹 2024-11-13 17:15:33

我已经使用 AsyncTask 完成了类似的任务。 AsyncTask 具有方法 onProgressUpdate(Integer),例如,您可以调用该方法,或者在 doInBackground() 期间每次完成进度时调用 <代码>publishProgress()。

有关更多详细信息,请参阅文档

I have done similar task using AsyncTask. AsyncTask has method onProgressUpdate(Integer) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().

Refer to the docs for more details.

脱离于你 2024-11-13 17:15:33
Thread t=new Thread()
{
public void run()
{
while(some condition)
{
sleep(1000);
Message myMessage = new Message();
myMessage.obj = "success";
handler.sendMessage(myMessage);
}
}
};


private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            String result=(String)msg.obj;
                     if(result.equals("success")
                     {
                     //update progress bar here
                      }

                 }
        };
Thread t=new Thread()
{
public void run()
{
while(some condition)
{
sleep(1000);
Message myMessage = new Message();
myMessage.obj = "success";
handler.sendMessage(myMessage);
}
}
};


private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            String result=(String)msg.obj;
                     if(result.equals("success")
                     {
                     //update progress bar here
                      }

                 }
        };
坏尐絯℡ 2024-11-13 17:15:33
  // this is demonstrate how to update progress bar from a thread 
  private ProgressBar pgb;  //  is a progressBar named with pgb 
  private int progressBarStatus = 0;// is a variable named progressBarStatus and initialize by 0
  // in  onCreate  method find the ui component view horizontal prograssbar named progressBar2  


 public void load_bar(View view) // is a button OnClick
// and i am using a thread with for loop to demonstrate a task and variable progressBarStatus  as status 

  pgb = (ProgressBar) findViewById(R.id.progressBar2) ;// find view by id 

  pgb.setMax(100); // at this point we the maximum value of a progress bar to make it full like we make 100 pices of it 

  pgb.setProgress(0); // initialize the progress bar with 0 % progress

    // progress bar handler that update the status of progress bar . without handler we cant update progress bar from thread 

   progressBarbHandler.post(new Runnable() {
                public void run() {
                    pgb.setProgress(progressBarStatus);
                } 
  // this is demonstrate how to update progress bar from a thread 
  private ProgressBar pgb;  //  is a progressBar named with pgb 
  private int progressBarStatus = 0;// is a variable named progressBarStatus and initialize by 0
  // in  onCreate  method find the ui component view horizontal prograssbar named progressBar2  


 public void load_bar(View view) // is a button OnClick
// and i am using a thread with for loop to demonstrate a task and variable progressBarStatus  as status 

  pgb = (ProgressBar) findViewById(R.id.progressBar2) ;// find view by id 

  pgb.setMax(100); // at this point we the maximum value of a progress bar to make it full like we make 100 pices of it 

  pgb.setProgress(0); // initialize the progress bar with 0 % progress

    // progress bar handler that update the status of progress bar . without handler we cant update progress bar from thread 

   progressBarbHandler.post(new Runnable() {
                public void run() {
                    pgb.setProgress(progressBarStatus);
                } 
晌融 2024-11-13 17:15:33

使用handler,该handler会更新进度条
你要做的是:
1)从您的线程向处理程序发送消息
2)更新处理程序中的进度条

Use handler, this handler will update the progress bar
you have to do is:
1)send message to handler from you thread
2)update progress bar in handler

浸婚纱 2024-11-13 17:15:33
private ProgressBar pgb;
private TextView textview;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    pgb = (ProgressBar) findViewById(R.id.progressBar2) ;
}

public void load_bar(View view)
{
    pgb.setMax(100);
    pgb.setProgress(0);

    new Thread(new Runnable() {
        @Override
        public void run() {
            for (progressBarStatus = 0; progressBarStatus <= 100; progressBarStatus++) {

                progressBarbHandler.post(new Runnable() {
                    public void run() {
                        pgb.setProgress(progressBarStatus);
                    }
                });


                try {
                    Thread.sleep(50);
                } catch (Exception ex) {
                }
            }
        }
    }).start();
}
private ProgressBar pgb;
private TextView textview;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    pgb = (ProgressBar) findViewById(R.id.progressBar2) ;
}

public void load_bar(View view)
{
    pgb.setMax(100);
    pgb.setProgress(0);

    new Thread(new Runnable() {
        @Override
        public void run() {
            for (progressBarStatus = 0; progressBarStatus <= 100; progressBarStatus++) {

                progressBarbHandler.post(new Runnable() {
                    public void run() {
                        pgb.setProgress(progressBarStatus);
                    }
                });


                try {
                    Thread.sleep(50);
                } catch (Exception ex) {
                }
            }
        }
    }).start();
}
夜唯美灬不弃 2024-11-13 17:15:33

使用 Thread 你可以像这样调用 oncreate 方法中的函数

public void loadProgressbar()
{
    progressBar.setMax(100);
    progressBar.setProgress(0);

    new Thread(new Runnable() {
        @Override
        public void run() {
            for (progress = 0; progress <= 100; progress++) {

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


                try {
                    Thread.sleep(50);
                } catch (Exception ex) {
                }
            }
        }
    }).start();
}

Using Thread you can do it like this just call the function in oncreate method

public void loadProgressbar()
{
    progressBar.setMax(100);
    progressBar.setProgress(0);

    new Thread(new Runnable() {
        @Override
        public void run() {
            for (progress = 0; progress <= 100; progress++) {

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


                try {
                    Thread.sleep(50);
                } catch (Exception ex) {
                }
            }
        }
    }).start();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文