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