在onPreExecute中显示ProgressDialog,但它同时在onPostExecute中显示和关闭

时间:2014-05-02 08:40:19

标签: android android-asynctask progressdialog

我希望在使用在活动类中扩展AsyncTask的私有类运行耗时的方法时显示ProgressDialog。但是当onPreExecute时,应该出现在onPreExecute中的ProgressDialog不会出现。而不是它,ProgressDialog在耗时的方法完成后立即显示和解除。

            public class ABC extends Activity {

            /*somewhere in ABC
                load = new Load();
                load.execute(a,b,c);
                int action = load.get();*/

            private class Load extends AsyncTask<Object, Integer, Integer>{ 

                    private ProgressDialog progDailog;

                    @Override
                    protected Integer doInBackground(Object... param) {
                        Log.v("!!!!!!Load!!!!!!!!!","doInBackground");
                        Object a = (Object) param[0];
                        int b = (Integer) param[1];
                        int c = (Integer) param[2];
                        return a.doSomething(b, c);
                    }

                    @Override
                    protected void onPostExecute(Integer result) {
                        super.onPostExecute(result);            
                        progDailog.dismiss();
                        Log.v("!!!!!!Load!!!!!!!!!","onPostExecute");
                    }

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        Log.v("!!!!!!Load!!!!!!!!!","onPreExecute");
                        progDailog = new ProgressDialog(ABC.this); 
                        progDailog.setMessage("Loading..."); 
                        progDailog.show();
                    }


                }

            }

下面是logcat:

05-03 00:38:49.202: V/!!!!!!Load!!!!!!!!!(14666): preExecute
05-03 00:38:49.202: D/ProgressBar(14666): setProgress = 0
05-03 00:38:49.202: D/ProgressBar(14666): setProgress = 0, fromUser = false
05-03 00:38:49.202: D/ProgressBar(14666): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
05-03 00:38:49.232: V/!!!!!!Load!!!!!!!!!(14666): doInBackground
05-03 00:38:53.662: I/Choreographer(14666): Skipped 269 frames!  The application may be doing too much work on its main thread.
05-03 00:38:53.762: D/ProgressBar(14666): updateDrawableBounds: left = 0
05-03 00:38:53.762: D/ProgressBar(14666): updateDrawableBounds: top = 0
05-03 00:38:53.762: D/ProgressBar(14666): updateDrawableBounds: right = 144
05-03 00:38:53.762: D/ProgressBar(14666): updateDrawableBounds: bottom = 144
05-03 00:38:53.772: V/!!!!!!Load!!!!!!!!!(14666): onPostExecute

2 个答案:

答案 0 :(得分:1)

这取决于doSomeThing方法的定义。由于此方法或您正在执行的操作的执行时间非常短。这就是为什么它会立即消失。但是如果你想看到对话或符合它是否正常工作。您可以使用doInBackground方法停留此方法。在您的doInBackground方法

中使用此代码
try{
Thread.sleep(3000);
}
catch(Exception e){
e.printStackTrace();
}

现在您将看到三秒钟的对话框。

答案 1 :(得分:0)

只需在异步类中更改方法的流程。

public class ABC extends Activity {

        /*somewhere in ABC
            load = new Load();
            load.execute(a,b,c);
            int action = load.get();*/

        private class Load extends AsyncTask<Object, Integer, Integer>{ 

                private ProgressDialog progDailog;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    Log.v("!!!!!!Load!!!!!!!!!","onPreExecute");
                    progDailog = new ProgressDialog(ABC.this); 
                    progDailog.setMessage("Loading..."); 
                    progDailog.show();
                }
                @Override
                protected Integer doInBackground(Object... param) {
                    Log.v("!!!!!!Load!!!!!!!!!","doInBackground");
                    Object a = (Object) param[0];
                    int b = (Integer) param[1];
                    int c = (Integer) param[2];
                    return a.doSomething(b, c);
                }

                @Override
                protected void onPostExecute(Integer result) {
                    super.onPostExecute(result);            
                    progDailog.dismiss();
                    Log.v("!!!!!!Load!!!!!!!!!","onPostExecute");
                }




            }

        }