【问题标题】:Show ProgressBar for a certain time in Android在Android中显示ProgressBar一段时间
【发布时间】:2012-10-02 13:44:51
【问题描述】:

我必须在我的 Android 应用程序中等待几秒钟,我想在此期间显示一个进度条,我该怎么做?

我试过这个代码:

    public boolean WaitTask() {

        pDialog = ProgressDialog.show(context,null, "Lädt..",true);
        new Thread() {
        public void run() {
            try{
                // just doing some long operation
                sleep(2000);
             } catch (Exception e) {  }
             pDialog.dismiss();
             }
         }.start();
        return true;
   }

但进度条会立即关闭,无需等待两秒钟。我的问题在哪里?

进度条应该类似于 Android 开发者在this 网站中显示的活动圈。

更新 异步任务

private class WaitTime extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog.show();
    }
    protected void onPostExecute() {
        mDialog.dismiss();
    }

@Override
protected void onCancelled() {
    mDialog.dismiss();
    super.onCancelled();
}

    @Override
    protected Void doInBackground(Void... params) {
        long delayInMillis = 2000;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mDialog.dismiss();
            }
        }, delayInMillis);
        return null;
    }
}

我这样称呼它:

        mDialog = new ProgressDialog(CreateProject.this);
        mDialog = ProgressDialog.show(context,null, "Lädt..",true);

        WaitTime wait = new WaitTime();
        wait.execute();

【问题讨论】:

  • 也许这篇文章:stackoverflow.com/questions/2798443/… 会有所帮助
  • 我尝试了前两种解决方案,但均未成功。进度条立即消失...
  • 根据您的 asynctask 更新:定时器的计划任务在后台运行,因此在 doInBackground 中计划定时器后立即调用 onPostExecute,因此立即关闭对话框,有关如何处理对话框的示例,请参见我的答案计时器。

标签: android progress-bar wait


【解决方案1】:

我建议你使用 AsyncTask,然后你可以这样做:

    AsyncTask<Void, Void, Void> updateTask = new AsyncTask<Void, Void, Void>(){
        ProgressDialog dialog = new ProgressDialog(MyActivity.this);
        @Override
        protected void onPreExecute() {
            // what to do before background task
            dialog.setTitle("Loading...");
            dialog.setMessage("Please wait.");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // do your background operation here
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // what to do when background task is completed
            dialog.dismiss();
        };

        @Override
        protected void onCancelled() {
            dialog.dismiss();
            super.onCancelled();
        }
    };
    updateTask.execute((Void[])null);

如果你想等待某个特定的时间,也许你想使用定时器:

    final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
    dialog.setTitle("Loading...");
    dialog.setMessage("Please wait.");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();

    long delayInMillis = 5000;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, delayInMillis);

【讨论】:

  • 将我的AsyncTask 添加到我的主要帖子中,但它不起作用。 ProgressBar 无需等待 2 秒就消失了,并且出现了与 Tal Kanel 的帖子中相同的 LogCat 错误
  • 我可以解决 LogCat 中的错误,但进度条仍然立即消失......
  • 当你混合了我的 AcyncTask 和 Timer 建议时,尝试从 onPostExecute 中删除 dialog.dismiss() 并让它被计时器解除。
【解决方案2】:

错误:应该从 UI 线程调用 pDialog.dismiss();,而不是从新线程调用。

所以你的代码应该改为:

pDialog = ProgressDialog.show(context,null, "Lädt..",true);
    new Thread() {
    public void run() {
        try{
            // just doing some long operation
            Thread.sleep(2000);
         } catch (Exception e) {  }
           // handle the exception somehow, or do nothing
         }

         // run code on the UI thread
         mYourActivityContext.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                pDialog.dismiss();
            }
        });

     }.start();

通常 - 有更好的方法来执行后台任务(等待两秒钟什么也不做也是后台任务)并在完成后在主 UI 线程中执行某些操作。例如,您可以使用 AsyncTask 类。最好使用这个 android 内置机制,而不是“原始”线程创建,尽管它也可以工作 - 前提是你能正确处理你的应用程序和活动生命周期。请记住,在您等待的两秒钟内,用户可能会离开您的应用程序。在这种情况下,dismiss(); 方法将在已销毁的上下文中调用...

我建议您阅读更多内容 - http://developer.android.com/reference/android/os/AsyncTask.html

【讨论】:

  • 你看到了 catch() 后面的 {} 吗? :-)
  • 如果我尝试运行您的代码,我会收到以下 LogCat 错误 10-02 16:06:01.380: E/WindowManager(15154): Activity de.bertrandt.bertrandtknx.CreateProject has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41aa7f10 that was originally added here 10-02 16:06:01.380: E/WindowManager(15154): android.view.WindowLeaked: Activity de.bertrandt.bertrandtknx.CreateProject has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41aa7f10 that was originally added here
  • @Mr.Mojo:阅读我在答案底部添加的内容。这正是我警告你的......
  • 睡眠是在线程上下文中调用的,因此它是完全有效的。 catch 块是在解雇之前。第三点是有效的,虽然
  • 为什么不使用 handler.postDelayed 而不是丑陋的睡眠,顺便说一句?
猜你喜欢
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
相关资源
最近更新 更多