Progress Dialog for AsyncTask - Android

前端 未结 4 816
情深已故
情深已故 2021-01-06 19:16

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 ap

4条回答
  •  猫巷女王i
    2021-01-06 19:51

    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> 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 
    {
    
      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();
    
    }
    

提交回复
热议问题