[JAVA] Programming from 51 years old Note Android Asynchronous communication callback Processing after AsyncTask ends Further generalization / standardization

Asynchronous communication callback

It is used when you want to do something after the processing of AsyncTask is completed.

MainActivity


public class MainActivity extends AppCompatActivity{

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);

        AsyncCallBack asyncCallBack = new AsyncCallBack();
        MyAsyncTask myAsyncTask = new MyAsyncTask(asyncCallBack);
        myAsyncTask.execute();
    }

    public class AsyncCallBack{
        public void callBack(String s){
            //Describe the process you want to do after the AsyncTask ends
            tv.setText(s);
        }
    }
}

MyAsyncTask


public class MyAsyncTask extends AsyncTask<Void,Void,String> {

    private MainActivity.AsyncCallBack callBack = null;

    public MyAsyncTask(MainActivity.AsyncCallBack callBack){
        this.callBack = callBack;
    }

    @Override
    protected String doInBackground(Void... aVoid) {
        return "success";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        callBack.callBack(s);
    }
}

SampleCode that generalizes / standardizes AnsycTask

It would be very difficult to write AsyncTask code every time asynchronous communication is performed.

From the caller

AsyncCallBack callBack  = new AsyncCallBack();
AsyncTask     asyncTask = new AsyncTask(callback);

I want to be able to call it from anywhere.

With the above "CallBack for asynchronous communication" method, in the constructor of AsyncTask I received an argument with \ <MainActivity.AsyncCallBack asyncCallBack>, but I can't generalize with this. So using Interface to be able to declare the AsyncCallBack type Generalize by accepting arguments with \ .

I will also use HashMap to make the parameters portable.
Then the interface!

CallBack.interface


interface CallBack{
   public void Receiver(Map<String,String> map);
}

Now you can use CallBack type arguments!

AsyncTaskSample.java


public class AsyncTaskSample extends AsyncTask<Map<String,String>,Void,Map<String,String>{

   AsyncTaskCallBack callback;

   public AsyncTaskSample(AsyncTaskCallBack callBack){
      this.callback = callBack;
   }
   
   @Override 
   protected Map<String,String> doInBackground(Map<String,String>... map){
      return map[0];
   }

   @Override 
   protected void onPostExecute(Map<String,String> map){
      super.onPostExecute(map);
      callback.Receiver(map);
   }
}

MainActivity.java



public class MainActivity extends AppCompatActivity implements CallBack{ //Extend CallBack

   @Override 
   protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      AsyncTaskSample  async = new AsyncTaskSample(this);
      Map<String,String> map = new HashMap<String,String>();
         map.put("key","MainActivity");
      async.execute(map);

      new SampleClass.SampleClass();
   }

   @Override 
   public void Receiver(Map<String,String> map) {
      Log.d("=====>",map.get("key");
   }

SampleClass.java


public class SampleClass implemants CallBack{ //Extend CallBack

   void SampleClass(){
      AsyncTaskSample  async = new AsyncTaskSample(this);
      Map<String,String> map = new HashMap<String,String>();
         map.put("key","SampleClass");
      async.execute(map); 
   }
   
   @Override 
   public void Receiver(Map<String,String> map){
      Log.d("=====>",map.get("key");
   }
}

Recommended Posts

Programming from 51 years old Note Android Asynchronous communication callback Processing after AsyncTask ends Further generalization / standardization
Programming from 51 years old Note AsyncTask --reference [copying sutras]
Programming from 51 years old Note --reference
Programming from 51 years old memorandum android Timer
Programming from 51 years old Note Thread summary
(Currently 52) programming from 51 years old Note 3 lines Move focus to android button
Programming from 51 years old Android memo Activity Manager memo
Programming from 51 years old (currently 52) Note Path and File
Programming from 51 years old Note: Do not start Android Sevice more than once Service start confirmation
Programming from 51 years old Note: Show multiple views in android dialog / with or without xml
Programming from 51 years old Memorandum code for network communication Socket | HttpURLConnection
Basic basis of Android asynchronous processing "AsyncTask"
Programming from 51 years old (currently 52) Note: Add jar with VScode Class.forName java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Drive