9

我必须在我的 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 开发人员在站点中显示的活动圈。

更新 异步任务

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();
4

2 回答 2

9

我建议您使用 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);

如果您想等待某个特定时间,也许您想使用 Timer:

    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);
于 2012-10-02T14:02:51.837 回答
6

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

因此您的代码应更改为:

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

于 2012-10-02T14:00:44.520 回答