These past couple of days I have been using AsyncTask quite a bit and am pretty amazed by how easy it is to use.
For those who aren't familiar with it yet, it is a class that allows operations to be done on the background thread and publish results on the UI (main) thread. It is an incredibly useful class and definitely should be in every Android developers toolkit.
For those who want a quick tutorial:
AsyncTask must be sub-classed to be used and override at least the doInBackground(..) method, otherwise the compiler will complain. You will usually be overriding onPostExecute(..) also.
AsyncTask takes in three parameters when you extend it ,
1) Params: The type of parameter sent to the AsyncTask when it executes.
2) Progress: The type of progress the AsyncTask returns while its executing in the background.
3) Result: The type of result that comes back after completion.
If you don't need any of these, you can type in "Void" to mark it as unused.
For those who aren't familiar with it yet, it is a class that allows operations to be done on the background thread and publish results on the UI (main) thread. It is an incredibly useful class and definitely should be in every Android developers toolkit.
For those who want a quick tutorial:
AsyncTask must be sub-classed to be used and override at least the doInBackground(..) method, otherwise the compiler will complain. You will usually be overriding onPostExecute(..) also.
AsyncTask takes in three parameters when you extend it ,
1) Params: The type of parameter sent to the AsyncTask when it executes.
2) Progress: The type of progress the AsyncTask returns while its executing in the background.
3) Result: The type of result that comes back after completion.
If you don't need any of these, you can type in "Void" to mark it as unused.
public class SampleTaskInBackground extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String result = "";
try{
for(int i = 0;i<5;i++) {
Thread.sleep(500); //Wait 0.5 seconds before every loop
result = result + strings[0]; //append
}
}catch (InterruptedException e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
sampleTextView.setText(s);
}
Here we will have our class, SampleTaskInBackground, which takes in a String input, and returns a String on completion. In its doInBackground(..) method, we have it loop through 5 times and append the input string onto itself every loop. After it is done executing, it will call onPostExecute(..) (which executes on the main thread) and it will have the returned string from doInBackground(..), and we set it to our TextView called sampleTextView.
To start our asynctask, we will call .execute(..):
To start our asynctask, we will call .execute(..):
new SampleTaskInBackground().execute("Testme");
Note: AsyncTask *MUST* to be executed from the main thread. So make sure that you aren't using another background thread to call your AsyncTask class.
Here we tell our AsnycTask to process our string "Testme." If implemented successfully, you should see your TextView with your brand new string, which in our case is :
"TestmeTestmeTestmeTestmeTestme"
Congratulations! You've just implemented a fully-working AsyncTask! I must say that there are a lot of benefits that the AsyncTask offers that I'm not covering here. This is just meant to be a quick and simple tutorial to those that wish to learn. For those who want a more complete and better tutorial, I would suggest the android docs: http://developer.android.com/reference/android/os/AsyncTask.html
I have uploaded a sample project here:
https://github.com/allenktv/AndroidDemos/tree/master/AsyncTask
Feel free to leave comments or contact me for any questions or concerns.
Good luck coding!
Here we tell our AsnycTask to process our string "Testme." If implemented successfully, you should see your TextView with your brand new string, which in our case is :
"TestmeTestmeTestmeTestmeTestme"
Congratulations! You've just implemented a fully-working AsyncTask! I must say that there are a lot of benefits that the AsyncTask offers that I'm not covering here. This is just meant to be a quick and simple tutorial to those that wish to learn. For those who want a more complete and better tutorial, I would suggest the android docs: http://developer.android.com/reference/android/os/AsyncTask.html
I have uploaded a sample project here:
https://github.com/allenktv/AndroidDemos/tree/master/AsyncTask
Feel free to leave comments or contact me for any questions or concerns.
Good luck coding!