How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

前端 未结 17 2154
鱼传尺愫
鱼传尺愫 2020-11-21 04:50

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute(

17条回答
  •  感动是毒
    2020-11-21 05:23

    try this:

    public class SomAsyncTask extends AsyncTask {
    
        private CallBack callBack;
    
        public interface CallBack {
            void async( JSONObject jsonResult );
            void sync( JSONObject jsonResult );
            void progress( Integer... status );
            void cancel();
        }
    
        public SomAsyncTask(CallBack callBack) {
            this.callBack = callBack;
        }
    
        @Override
        protected JSONObject doInBackground(String... strings) {
    
            JSONObject dataJson = null;
    
            //TODO query, get some dataJson
    
            if(this.callBack != null)
                this.callBack.async( dataJson );// asynchronize with MAIN LOOP THREAD
    
            return dataJson;
    
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
    
            if(this.callBack != null)
                this.callBack.progress(values);// synchronize with MAIN LOOP THREAD
    
        }
    
        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
    
            if(this.callBack != null)
                this.callBack.sync(jsonObject);// synchronize with MAIN LOOP THREAD
        }
    
        @Override
        protected void onCancelled() {
            super.onCancelled();
    
            if(this.callBack != null)
                this.callBack.cancel();
    
        }
    }
    

    And usage example:

    public void onCreate(@Nullable Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
    
             final Context _localContext = getContext();
             SomeAsyncTask.CallBack someCallBack = new SomeAsyncTask.CallBack() {
    
                    @Override
                    public void async(JSONObject jsonResult) {//async thread
                        //some async process, e.g. send data to server...
                    }
    
                    @Override
                    public void sync(JSONObject jsonResult) {//sync thread
                        //get result...
    
                        //get some resource of Activity variable...
                        Resources resources = _localContext.getResources();
                    }
    
                    @Override
                    public void progress(Integer... status) {//sync thread
                        //e.g. change status progress bar...
                    }
    
                    @Override
                    public void cancel() {
    
                    }
    
                };
    
                new SomeAsyncTask( someCallBack )
                                    .execute("someParams0", "someParams1", "someParams2");
    
        }
    

提交回复
热议问题