Can't see ProgressDialog while AsyncTask run in background

不羁岁月 提交于 2019-12-04 05:44:58

问题


I use AsyncTask in my App for download a url. I use a ProgressDialog on onPreExecute() for waiting. But I cant see ProgressDialog while process finish and i see it for a moment. want to see it while downloading not after that. can any one help me. thanks my code is like this:

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

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage("Please Wait ...");
        pDialog.isIndeterminate();
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Void doInBackground(Void... unused) {
        runOnUiThread(new Runnable() {
            public void run() {

           // do something for downloading

        }
        });

        return (null);
    }


    protected void onPostExecute(Void unused) {
        // closing progress dialog
        pDialog.dismiss();
    }
}

回答1:


Firstly notice the "@override" header attached to all the AsyncTask Implemented methods e.g.

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

    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage("Please Wait ...");
        pDialog.isIndeterminate();
        pDialog.setCancelable(false);
        pDialog.show();
    }

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

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pDialog.cancel();

    }

}

Also Remove this from doInBackground unless you must do something on the UI.

runOnUiThread(new Runnable() {
        public void run() {

       // do something for downloading

    }
    });

You cannot do something for downloading on the runOnUiThread. doInBackground is meant for running background tasks like downloads etc. not visible to the UI.




回答2:


 runOnUiThread(new Runnable() {
            public void run() {
           // do something for downloading
        }

// do something for downloading, inside runOnUiThread, is wrong. runOnUiThread makes "do something for downloading" run on the UI Thread, and your application should crash for NetworkOnMainThreadException, you the app runs on a device with a version of android grater than GingerBread. Differently it will block the ui thread preventing him to draw your progress bar




回答3:


The problem is in

runOnUiThread(new Runnable() {

        public void run() {

       // do something for downloading

    }
    });  

ProgressDialog will not update if the UI thread is still busy. There are many examples in SO for that. I don't understand why do you need UIthread. And as a rule of thumb - if you need Progress dialog, you need to let run asynctask in background thread,as it always do.Read the document

http://developer.android.com/reference/android/os/AsyncTask.html

You can use the below example

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/



来源:https://stackoverflow.com/questions/25752210/cant-see-progressdialog-while-asynctask-run-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!