AsyncTask
is one of the classes that implements competition in the Android and basically, it helps other classes like: Thread.
Currently, any event or modifying which occurs in a app, is managed in Main Thread of OS. This means that, for example, if you perform a long-term operation on the application, such as downloading a file, it will cause the Main Thread be it stuck until its action is complete. IE, your application will get stuck, the user will not be able to do anything in it, and will only unlock when the file download is completed.
One Asynctask avoids just that. It allows you to create instructions on background and Sincronize these instructions with the Main Thread, that is, the user will be able to continue using the application normally, it will not be locked and you will also be able to update the UI in the course of the process.
Basically, this class doesn’t interfere with Main Thread.
Arguments
The class Asynctask possesses 3 arguments to be implemented, are they:
AsyncTask<ParamsType, ProgressValue, ResultValue> {}
- Paramstype: Is the guy from parameter which will be sent to the instruction. This parameter will be sent to the method
doInBackground()
.
- Progressvalue: Like the name says, it’s kind of the value of our progress instruction. It will show the current progress of what we are trying to accomplish.
- Resultvalue: It’s the kind of final result we’ll get in our instruction. It is the result as a whole. It is returned from the method
doInBackground()
.
Methods
doInBackground()
This method is responsible for two things. The first, it is responsible for receiving the type of parameter you want as a result and the second, it is responsible for the code that will execute our instruction. For example, if we wish to make a Httprequest, this method will be responsible for the code block of the Httprequest and finally, after executing all our code, he will return a value, which is the outworking we wish to see. If we want as a result a String
, he will return a String
which will be sent to the method onPostExecute.
onPostExecute()
This method is called after the termination of the instructions in the method doInBackground, that is, when everything is complete in the Ackground method, when we have already received the parameter we want to receive, the Onpostexecute method will be called and it will show the result to the user, if you wish. It takes the parameter you set in the class.
Example
class MTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... result) {
// Http....
return mHttpResponse;
}
@Override
protected void onPostExecute(String resultValue) {
new Toast.makeText(context, resultValue, Toast.LENGTH_SHORT).show();
}
}
Links Úteis: https://developer.android.com/reference/android/os/AsyncTask.html#onPreExecute()
There is a very good explanation in Soen here
– mercador
@merchant if you manage to bring to the Sopt, most likely will be rewarded for it =D
– viana
Lesson 3 of this course explains perfectly everything about Asynctask: https://br.udacity.com/course/android-basics-networking-ud843/
– Márcio Oliveira
Related: Thread or Asynctask? When and which one should I use?
– ramaral