ProgressDialog出现得太晚并且消失得太快

发布于 2024-12-26 09:46:20 字数 1102 浏览 0 评论 0原文

当我更新数据库时,我想显示一个进度对话框。我的问题是,ProgressDialog 出现得很晚,4-5 秒后,然后出现和消失得非常快,它在屏幕上停留几毫秒,几乎你看不到它,然后新数据立即显示在列表中。这让我认为 ProgressDialog 正在等待数据库更新(不需要太多时间,大约 4.5 秒),然后它显示在屏幕上,但很快就消失了。我希望按下“更新”按钮后立即出现 ProgressDialog,并在屏幕上停留大约 4-5 秒。

class MyAsyncTask extends AsyncTask<Void, Void, Void>{

        ProgressDialog myprogsdial;
        @Override
        protected void onPreExecute(){
            myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

                runOnUiThread(new Runnable() {

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

                        RefreshDataBase();

                    }
                });

            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            myprogsdial.dismiss();
        }

    }

当我调用它时,new MyAsyncTask().execute();

While I'm updating my database I want to display a progress dialog. My problem is that the ProgressDialog is getting late to appear,after 4-5 seconds, then appears and disappears very fast, it stays on screen few milliseconds almost you can't see it, then new data are shown in the list immediately. This makes me think that the ProgressDialog is waiting for database to be updated(it doesn't take much, about 4,5 seconds) and then it shows on the screen but is dismissing very fast. I would like the ProgressDialog appear immediately I press the 'Update' button and stay on the screen about 4-5 seconds.

class MyAsyncTask extends AsyncTask<Void, Void, Void>{

        ProgressDialog myprogsdial;
        @Override
        protected void onPreExecute(){
            myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

                runOnUiThread(new Runnable() {

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

                        RefreshDataBase();

                    }
                });

            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            myprogsdial.dismiss();
        }

    }

When I call it, new MyAsyncTask().execute();

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

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

发布评论

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

评论(3

離殇 2025-01-02 09:46:20

好吧,我认为这个

runOnUiThread(new Runnable() {

导致了这种行为。doInBackground

() 在主 UI 线程的新线程中执行代码。然后,您将此线程中要执行的代码放回到主线程中,从而导致进度对话框最后延迟,然后在 postExecute() 中它立即关闭

。 rel="noreferrer">asyntask 教程可以在这里找到。

Ok I think that this

runOnUiThread(new Runnable() {

is causing this behavior.

doInBackground() executes your code in a new thread to the main UI thread. You are then putting the code to execute in this thread back into the main one causing the progress dialog to be delayed at the end and then in postExecute() it gets closed immediately.

A good asyntask tutorial can be found here.

世界如花海般美丽 2025-01-02 09:46:20

您不得使用runOnUiThread。您基本上所做的是:

  1. 启动新的非 ui 线程
  2. 从这个新的非 ui 线程中,您向 UI 线程发布了一个长时间运行的任务。
  3. 从非 ui 线程退出。
  4. 您的 ui 线程现在执行长时间运行的操作 (RefreshDataBase) 并阻止 UI。

您应该直接调用RefreshDataBase()。如果这个方法涉及UI,你就必须重构它。

You must not use runOnUiThread. What you're basically did is:

  1. Started new non-ui thread
  2. From this new non-ui thread you posted a long running task to UI thread.
  3. Exited from non-ui thread.
  4. Your ui thread now executes long-running operation (RefreshDataBase) and blocks the UI.

You should call RefreshDataBase() directly. And if this method touches UI, you have to refactor it.

不一样的天空 2025-01-02 09:46:20

我已经使用弗拉基米尔·伊万诺夫的这个答案解决了这个问题。
我已将功能与外观分开。
我将功能(下载新数据)保留在 doInBackground() 中,并在 onPostExecute() 中更新了列表:获取新的适配器,称为 setListAdaper()notifyDataSetChanged
当然,我退出了 runOnUiThread()。感谢大家的提示。

I have solved it, using this answer of Vladimir Ivanov.
I have separated the functionality by the appearance.
I have kept the functionality(downloading new data) in doInBackground() and in onPostExecute() I updated the list: get the new adapter,called setListAdaper() and notifyDataSetChanged.
Of course, I quit using runOnUiThread(). Thanks to all for hints.

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