External AsyncTask class with ProgressDialog [Update: and returning back?]

后端 未结 3 1455
悲&欢浪女
悲&欢浪女 2020-12-09 23:37

**Updated: (See below)**I have been looking around for couple of days and can\'t find a straight answer to this. Some say it possible to some say to accomplish some say it\

相关标签:
3条回答
  • 2020-12-10 00:20

    ok this is what I did as i was using fragments. This is how you call the AsyncTask inside a fragment:

    String result=new AsyncClass(getActivity()).execute();
    

    and this is how my AsyncTask outer class looks like:

    public class AsyncClass extends AsyncTask<Void, Void, String> {
    
        ProgressDialog pdialog;
    
        public AsyncClass(Context context) {
            pdialog = new ProgressDialog(context);
        }
    
        @Override
        protected void onPreExecute() {
            pdialog.setIndeterminate(true);
            pdialog.setCancelable(false);
            pdialog.setTitle("Loading Feed..");
            pdialog.setMessage("Please wait.");pdialog.show();
        }
    
        @Override
        protected String doInBackground(Void... params) {
            String result=null;
            //do your task here and generate result String
            return result;
        }
    
        @Override
        protected void onPostExecute(String result) {
            if(pdialog.isShowing())
                 pdialog.dismiss();
        }
    }
    

    @SSZero thanks great answer, helped a lot.

    0 讨论(0)
  • 2020-12-10 00:20

    I would like to answer that follow up question i.e.

    I have a follow up question: Using the above code, is it possible to return a value from the onPostExecute method back to the main class, somehow? (Sorry about beeing noobish) I have tried something like this:

    String result = new AsyncClass(this).execute();

    I did this in my code it worked.

    AsyncClass ac=new AsyncClass();
                        
    ac.execute("");
    
    String rslt=ac.get();
    

    Call this code wherever you want to.

    public class AsynchClass extends AsyncTask <String,Integer,String>
    {
    
        public String result=null;
    
    
        @Override
        protected void onPreExecute() {
        // TODO Auto-generated method stub  
         
        }
    
        protected String doInBackground(String... params) 
        {
    
                // Do all your background task here
    
                return result;
                        
        }
        @Override
         
        protected void onProgressUpdate(Integer... values) {
        
          
        }
    
        protected void onPostExecute(String result) {
        
        }
    }
    
                    
                 
    
    0 讨论(0)
  • 2020-12-10 00:22

    You were creating the ProgressDialog with a null context. The following code worked for me.

    public class AsyncClass extends AsyncTask<Void, String, Void> {
        private Context context;
        ProgressDialog dialog;
    
            public AsyncClass(Context cxt) {
                context = cxt;
                dialog = new ProgressDialog(context);
            }
    
            @Override
            protected void onPreExecute() {
                dialog.setTitle("Please wait");
                dialog.show();
            }
    
            @Override
            protected Void doInBackground(Void... unused) {
                SystemClock.sleep(2000);
                return (null);
            }
    
            @Override
            protected void onPostExecute(Void unused) {
                dialog.dismiss();
            }
        }
    
    0 讨论(0)
提交回复
热议问题