ProgressDialog.dismiss() 和 AlertDialog.show() 之间的长时间延迟

发布于 2024-11-18 18:32:46 字数 1005 浏览 7 评论 0原文

我使用的是标准 ProgressDialog 实现:我设置了一个线程来运行一个长任务,完成后它会关闭 ProgressDialog。

@Override
public void onCreate( Bundle bundle ) {

        super.onCreate( bundle );

        context = this;
        progress = ProgressDialog.show( this, "Running", "Please wait..", true, false);
        progress.setOnDismissListener( new OnDismissListener() {

            public void onDismiss(DialogInterface dialog) {

                showResults();
            }

        });

        new DeleteThread().start();
}

线程如下所示:

private class DeleteThread extends Thread {

    public DeleteThread() {}

    @Override
    public void run() {

        // long process during which we populate
        // a LinearLayout with many other Views 
        progress.dismiss();
    }
}

showResults() 中,我们采用现在填充有视图的 LinearLayout 并将其设置为 AlertDialog 的内容。

问题是,ProgressDialog 消失了,并且在弹出 AlertDialog 之前仍然有很长一段时间(10-12 秒)什么也没有发生。有没有办法立即实现这种转变?

I'm using a standard ProgressDialog implementation: I set up a thread to run a long task, and when it's done it dismisses the ProgressDialog.

@Override
public void onCreate( Bundle bundle ) {

        super.onCreate( bundle );

        context = this;
        progress = ProgressDialog.show( this, "Running", "Please wait..", true, false);
        progress.setOnDismissListener( new OnDismissListener() {

            public void onDismiss(DialogInterface dialog) {

                showResults();
            }

        });

        new DeleteThread().start();
}

And the Thread looks like this:

private class DeleteThread extends Thread {

    public DeleteThread() {}

    @Override
    public void run() {

        // long process during which we populate
        // a LinearLayout with many other Views 
        progress.dismiss();
    }
}

And in showResults() we take the LinearLayout now filled with Views and set it as the content of an AlertDialog.

The problem is, the ProgressDialog goes away and there's still a long period of time (10-12sec) where nothing is happening before the AlertDialog pops up. Is there a way to make this transition instantaneously?

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

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

发布评论

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

评论(3

鹤仙姿 2024-11-25 18:32:46

我知道这个答案可能来得有点晚,但那又怎么样。

我看到您正在从 onDismiss 回调中触发 showResults() 方法。我建议你反过来做。为此,您可能需要使用 AsyncTask 类。

AsyncTask 允许您轻松设置任务以在生成的线程中运行,根据需要更新进度条(不是必需的,但在您的情况下可能是一个不错的选择),当然还可以在生成后在 UI 线程上运行一些代码线程完成。对于您的情况,我会将您的布局视图填充放在 AsyncTask.doInBackground() 方法中。然后,您需要在 AsyncTask.onPostExecute() 中添加 showResults() 方法调用。在 onPostExecute() 的底部,您应该关闭进度对话框。这应该会给你你想要的结果。

作为奖励,您可能需要为 AsyncTask 类创建一个构造函数,并将 ProgressDialog 的创建放在其中。这样,ProgressDialog 将完全封装在该类中,一切都干净整洁。所以,像这样:

class MyAsyncClass extends AsyncClass
{
  ProgressDialog m_progress;

  public MyAsyncClass()
  {
    m_progress = ProgressDialog.show( this, "Running", "Please wait..", true, false);
  }

  protected Long doInBackground(Object... data) {
    // Do your long process of populating a LinearLayout with many other Views 
  }

  protected void onPostExecute(Long result) {
    showResults();
    m_progress.dismiss();
  }
}

注意:上面的类需要一些额外的参数,但你明白了。

I know this answer might be coming in a bit late, but what the heck.

I see that you're triggering the showResults() method from the onDismiss callback. I would suggest you do it the other way around. To do that you may need to use the AsyncTask class.

The AsyncTask allows you to easily setup tasks to run in a spawned thread, to update a progress bar if needed (not required, but might be a nice touch in your case) and of course to run some code on the UI thread once the spawned thread completes. For your case, I would place your populating of layoutview in the AsyncTask.doInBackground() method. Then, you'd want to add the showResults() method call in AsyncTask.onPostExecute(). At the bottom of onPostExecute() you should then dismiss your progress dialog. That SHOULD give you the results you want.

As a bonus, you might want to create a constructor for your AsyncTask class and place the creation of the ProgressDialog in there. That way the ProgressDialog will be completely encapsulated within that class and everything is nice and clean. So, something like this:

class MyAsyncClass extends AsyncClass
{
  ProgressDialog m_progress;

  public MyAsyncClass()
  {
    m_progress = ProgressDialog.show( this, "Running", "Please wait..", true, false);
  }

  protected Long doInBackground(Object... data) {
    // Do your long process of populating a LinearLayout with many other Views 
  }

  protected void onPostExecute(Long result) {
    showResults();
    m_progress.dismiss();
  }
}

NOTE: Above needs some extra parameters to the class, but you get the idea.

梦初启 2024-11-25 18:32:46

作为一名新手 Android 开发人员,我将大胆猜测一下,并建议虽然为 LinearLayout 生成了指针,但布局本身尚未填充内容,因此当您调用 Progress.dismiss() 时,布局本身就会填充内容,

您可能会必须在设置 AlertDialogs 内容之前渲染视图。 http://developer.android.com/guide/topics/ui /how-android-draws.html 可能在这方面有一些帮助,不幸的是我的笔记本电脑有硬盘问题,所以我没有安装 Android 开发环境,无法尝试。但我会在 Progress.dismiss() 之前尝试对 LinearLayout 进行 invalidate,我希望有所帮助:S

I'm going to venture a guess as a novice Android developer and suggest that while the pointers are generated for the LinearLayout, the layout itself hasn't been populated with content and therefor is populating when you call progress.dismiss()

You will probably have to render the view prior to setting to the AlertDialogs content. http://developer.android.com/guide/topics/ui/how-android-draws.html might have some help in that respect, unfortunately my laptop is have HDD issues so I don't have the Android Development Environment installed and can't try it. But I'd try an invalidate on the LinearLayout right before progress.dismiss(), I hope that helps :S

寄人书 2024-11-25 18:32:46

使用如下所示的内容并使用 AsyncTask 来在完成后关闭对话框:

final ProgressDialog progressDialog = new ProgressDialog(v.getContext());

progressDialog.setCancelable(false);
progressDialog.setMessage(v.getContext().getText(R.string.defaultQuantities));
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);

new CountDownTimer(250, 250) {
    public void onTick(long millisUntilFinished) {
    }

    public void onFinish() {
        if (progressDialog.getProgress() < 100) {
        progressDialog.show();
        }
    }
}.start();

Make use of something like this below and use an AsyncTask that dismisses the dialog once it is done:

final ProgressDialog progressDialog = new ProgressDialog(v.getContext());

progressDialog.setCancelable(false);
progressDialog.setMessage(v.getContext().getText(R.string.defaultQuantities));
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);

new CountDownTimer(250, 250) {
    public void onTick(long millisUntilFinished) {
    }

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