Android:ProgressDialog 失败,版本 2

发布于 2024-12-01 09:13:55 字数 1949 浏览 2 评论 0原文

我之前问过类似的问题,但我仍然遇到麻烦。 ProgressDialog 导致我严重脱发。这段代码有一个进度对话框(旋转器),只是为了看看我是否可以让一个进度对话框出现 - 这只会导致我打开文件之前有 10 秒的延迟。

我有一个列表活动。用户选择一个文件,这将我们带到这里:

private void openFile(File f) {
    Global.iDelay = 0;

    // Create an Intent
    Global.file = f;

    dialog = ProgressDialog.show(viewer2.this, "", "Loading...");
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.show(); // dialog does not appear //

    // create a thread for counting the sleep seconds
    Thread background = new Thread (new Runnable() {
       public void run() {
           try {

              while (Global.iDelay < 10) {
                   // wait between each update
                   Thread.sleep(1000);

                   Log.d("Baz","iDelay = "+Global.iDelay);

                   // active the update handler
                   progressHandler.sendMessage(progressHandler.obtainMessage());
                   }

           } catch (java.lang.InterruptedException e) {
               // if something fails do something smart
           }
       }
    });

    // start the background thread
    background.start();

    dialog.dismiss();

    //--------------------------------------------------------------
    // then I go and prepare for my file and open it        


    Global.PrepareForGL (this); 

    Intent myIntent1 = new Intent(this, ShowGL.class);
    this.startActivity(myIntent1);
    }

我的处理程序定义如下:

//handler for the background updating
public static Handler progressHandler = new Handler() {
    public void handleMessage(Message msg) {
        Global.iDelay++;
    }
};

我的问题:计时器线程启动并且足够计数 10 秒(我的 log.d 给我计数到 10),但没有出现对话框。然而,在 10 秒计数期间,我已经在执行“prepareforgl”方法,据我所知,直到后台线程完成并且对话框被关闭后,该方法才会发生。我知道这一点是因为它还包含一个 log.d 行,它为我提供了 5 秒和 6 秒计数器之间的结果。

我的问题:我在列表活动中执行此操作是否相关?我的对话框在哪里?我是否正确地认为整个后台线程应该在我继续工作之前执行?

如果您能对进度对话框有所了解,我们将非常欢迎。非常感谢。

巴兹。

I asked a similar Q to this before but I'm still in trouble. ProgressDialog is causing me severe hair loss. This code has a progressdialog (spinner) just to see if I can make one appear - which just causes a 10 second delay before I then go and open my file.

I have a listactivity. The user selects a file, which brings us here:

private void openFile(File f) {
    Global.iDelay = 0;

    // Create an Intent
    Global.file = f;

    dialog = ProgressDialog.show(viewer2.this, "", "Loading...");
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.show(); // dialog does not appear //

    // create a thread for counting the sleep seconds
    Thread background = new Thread (new Runnable() {
       public void run() {
           try {

              while (Global.iDelay < 10) {
                   // wait between each update
                   Thread.sleep(1000);

                   Log.d("Baz","iDelay = "+Global.iDelay);

                   // active the update handler
                   progressHandler.sendMessage(progressHandler.obtainMessage());
                   }

           } catch (java.lang.InterruptedException e) {
               // if something fails do something smart
           }
       }
    });

    // start the background thread
    background.start();

    dialog.dismiss();

    //--------------------------------------------------------------
    // then I go and prepare for my file and open it        


    Global.PrepareForGL (this); 

    Intent myIntent1 = new Intent(this, ShowGL.class);
    this.startActivity(myIntent1);
    }

My handler is defined like this:

//handler for the background updating
public static Handler progressHandler = new Handler() {
    public void handleMessage(Message msg) {
        Global.iDelay++;
    }
};

My problem: The timer thread starts and suire enough counts off 10 seconds (my log.d gives me a count to 10), but no dialog appears. However, during the 10 second count I am already executing the 'prepareforgl' method, which I understood would not happen until the background thread has completed and the dialog been dismissed. I know this because that also contains a log.d line which gives me a result between the 5 and 6 second counters.

My Questions: Is it relevant that I'm doing this in a listactivity? Where is my dialog? Am I right in thinking the entire background thread should have executed before I then carry on with my work?

Any light you can shed on progressdialogs would be very welcome. Many thanks.

Baz.

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

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

发布评论

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

评论(1

扭转时空 2024-12-08 09:13:55

您正在启动后台线程,然后关闭该对话框。因此,您实际上并没有延迟关闭对话框的调用。它可能发生得太快,你来不及看到。

如果您使用 AsyncTask 来执行此操作,您的代码会更加干净。将延迟放在 doInBackground 方法中,将对话框关闭放在 onPostExecute 中。

此外,您还将创建和显示对话框的静态调用与更改和重新显示对话框的其他调用混合在一起。试试这个:

dialog = new ProgressDialog(viewer2.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
dialog.show();

You are starting your background thread and then dismissing the dialog. So, you aren't actually delaying the call to dismiss the dialog. It might just be happening too fast for you to ever see it.

Your code would be much cleaner if you use AsyncTask to do this. Put the delay in the doInBackground method, and the dialog dismissal in onPostExecute.

Also, you are mixing the static call to create and show the dialog with other calls to change and re-display the dialog. Try this instead:

dialog = new ProgressDialog(viewer2.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
dialog.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文