I was using AsyncTask somehow, so I tried to summarize the basics again.
Synchronous processing: To execute certain tasks in order. Asynchronous processing: When one task is executing, another task executes another processing.
Let's consider the case where asynchronous processing is not performed. When the user is operating on Android, Swift, web application, etc., for example, when acquiring some information (video list, search result list, etc.), if it is synchronous processing, other than UI etc. until the acquisition process is completed Cannot be processed at all and the screen remains stationary. If that happens, the user will feel that the app has stopped (bug).
If implemented by asynchronous processing, it is possible to perform information acquisition (video list, search result list, etc.) processing in parallel while making the UI / screen look good. By doing so, you can show the user that you are getting information now and avoid misunderstandings.
If you look closely, you can often see a loading screen that moves on the screen while loading Youtube and SNS.
You can use Handler and ThreadHandler classes for Android asynchronous processing, and AsyncTask is one of these Helper Classes.
AsyncTask is easier to handle async than any other method.
onPreExecute() It is executed in the main thread before executing the doInBackground method. Used when you want to perform some processing before asynchronous processing.
doInBackground() It runs in a thread separate from the main thread. Describe the content you want to process asynchronously.
onProgressUpdate() It runs on the main thread. Returns the progress to the main thread. It can be used when you want to display the progress of asynchronous processing with a progress bar.
onPostExecute() It is executed in the main thread after executing the doInBackground method. Returns the result to the main thread. You can receive the return value of the doInBackground method as an argument of this method and reflect the result on the screen.
class asyncTask extends android.os.AsyncTask{
@Override
protected Object doInBackground(Object... obj){
}
class asyncTask extends android.os.AsyncTask{
//* This argument is Object...However, as usual, you can also specify the type and number.
@Override
protected Object doInBackground(Object... obj){
//Fill in the process here
System.out.plintln("The contents of asynchronous processing are described here.")
//You can also get the arguments
String message = (String)obj[0];
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Task generation
mAsyncTask = new asyncTask(textView);
//Task execution
mAsyncTask.execute("Arguments you want to pass to asyncTask here");
}
}
If you process the main thread directly with doInBackground, an exception will occur.
・ AsyncTask can be used for asynchronous processing ・ AsyncTask can use asynchronous processing more easily than other methods. ・ DoInBackground is required when using AsyncTask
Recommended Posts