简体   繁体   中英

Progress Dialog for AsyncTask - Android

I'm trying to show a progress dialog while the twitter feed is loading up...However the progress dialog remains on screen when the twitter feed appears. Any help is much appreciated.

public class MainActivity extends ListActivity {

    final static String twitterScreenName = "CFABUK";
    final static String TAG = "MainActivity";
    private AsyncTask<Object, Void, ArrayList<TwitterTweet>> tat;
    boolean done;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        done=false;
        AndroidNetworkUtility androidNetworkUtility = new AndroidNetworkUtility();
        if (androidNetworkUtility.isConnected(this)) {
            TwitterAsyncTask syn=new TwitterAsyncTask();
            syn.execute(twitterScreenName,this);


            ProgressDialog pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("loading");
            pd.show();

            do {
                if(!(syn.getStatus()==AsyncTask.Status.RUNNING)) {
                pd.dismiss();
                pd.cancel();
                done=true;
                }
            } while(done=false);


        } else {
            Log.v(TAG, "Network not Available!");
        }
    }
}

You must call ProgressDialog show() method on AsyncTasks onPreExecute(). For example:

class MyTask extends AsyncTask<Void, Void, Void> {
 ProgressDialog pd;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
       pd = new ProgressDialog(MainActivity.this);
       pd.setMessage("loading");
       pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // Do your request
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if (pd != null)
      {
         pd.dismiss();
      }
    }
  }

You must use a onPreExecute and onPostExecute of AsyncTask class. For example:

class AsyncData extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // init progressdialog
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // get data
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // dismiss dialog
    }
}

The methods onPreExecute() , doInBackground() and onPostExecute() of AsyncTask are used for purpose that you mentioned -

public class MainActivity extends ListActivity {

    final static String twitterScreenName = "CFABUK";
    final static String TAG = "MainActivity";
    private AsyncTask<Object, Void, ArrayList<TwitterTweet>> tat;
    boolean done;
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        done=false;
        context = this;
        new NetworkTask().execute();
    }
}


class NetworkTask extends AsyncTask<String, String, String> 
{

  Context ctx = context ;

  @Override
  protected void onPreExecute() 
  {
    super.onPreExecute();
    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Working ...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();
}

@Override
protected String doInBackground(String... args) 
{

    //Do your background work here and pass the value to onPostExecute

    AndroidNetworkUtility androidNetworkUtility = new AndroidNetworkUtility();

    if (androidNetworkUtility.isConnected(ctx)) {
            TwitterAsyncTask syn=new TwitterAsyncTask();
            syn.execute(twitterScreenName,this);

             while(done)
             {
                if(!(syn.getStatus()==AsyncTask.Status.RUNNING)) 
                {
                   done=true;
                }
                else 
                {
                   Log.v(TAG, "Network not Available!");
                } 
             }

    return done + "";

}

protected void onPostExecute(String result) 
{

   //Do something with result and close the progress dialog

   pDialog.dismiss();

}

ProgressBar is best alternative for ProgressDialog. A user interface element that indicates the progress of an operation. ProgressDialog is deprecated in latest versions.

For more info see android developer official site: https://developer.android.com/reference/android/widget/ProgressBar.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM