在Android中显示ProgressBar一段时间

Mok*_*pps 9 android wait progress-bar

我必须在我的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;
   }
Run Code Online (Sandbox Code Playgroud)

但是进度条立即关闭而不等待两秒钟.我的问题在哪里?

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

更新 AsyncTask

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我称之为:

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

        WaitTime wait = new WaitTime();
        wait.execute();
Run Code Online (Sandbox Code Playgroud)

Ber*_*ťák 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);
Run Code Online (Sandbox Code Playgroud)

如果你想等一段特定的时间,也许你想使用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);
Run Code Online (Sandbox Code Playgroud)


Tal*_*nel 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();
Run Code Online (Sandbox Code Playgroud)

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

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