更新android中的进度条

发布于 2024-11-30 22:08:44 字数 1213 浏览 1 评论 0原文

我正在活动中启动一个线程并立即显示进度对话框。该线程中的方法在完成其任务后调用其他线程,该线程调用 m_progressDialog.dismiss(); 方法。

这工作正常,但我现在想实现进度条,该进度条如何根据上一个线程中的任务完成情况填充。完成其操作需要无限的时间。

这听起来可能模棱两可,但如果需要的话,我准备进一步详细说明。

一个小代码片段:

第一个线程中的方法像这样结束

...
runOnUiThread(m_returnRes);
}

private Runnable m_returnRes = new Runnable() {

    public void run() {
        m_progressDialog.dismiss();

我也尝试这样做

m_progressDialog = new ProgressDialog(this);
        m_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        m_progressDialog.setProgress(0);
        m_progressDialog.setMax(100);
        m_progressDialog.setMessage("Retrieving data..");
        m_progressDialog.show();

    Thread background = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub

            try {while(m_progressDialog.getProgress()<=100){
                Thread.sleep(500);}
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });

    background.start();

,但栏没有被填充,并且在调用 .dismiss() 方法后直接被解雇

I am starting a thread in my activity and immediately displaying progress dialog.The method in this thread after completing its task calls other thread which calls m_progressDialog.dismiss(); method.

This works fine but i want to implement progress bar now how does this bar fills according to completion of task in previous thread.It takes indefinite amount of time for completing its operations.

this could sound ambigious but iam ready to elaborate it further if required.

A little code snippet:

Method in first thread ends like this

...
runOnUiThread(m_returnRes);
}

private Runnable m_returnRes = new Runnable() {

    public void run() {
        m_progressDialog.dismiss();

Also i've tried to do it this way

m_progressDialog = new ProgressDialog(this);
        m_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        m_progressDialog.setProgress(0);
        m_progressDialog.setMax(100);
        m_progressDialog.setMessage("Retrieving data..");
        m_progressDialog.show();

    Thread background = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub

            try {while(m_progressDialog.getProgress()<=100){
                Thread.sleep(500);}
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });

    background.start();

but the bar does not gets filled and gets dismissed directly after .dismiss() method is called

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

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

发布评论

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

评论(1

ゞ花落谁相伴 2024-12-07 22:08:44

尝试使用 AsyncTask。它为自己处理线程,并在 onProgressUpdate 更改视图内容,您可以在其中更新进度栏。

Try using AsyncTask. It handles threding for itself and has function publishprogress inside the onProgressUpdate to change view content, where you could update progress bar.

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