Android Tips#1 Services & AsyncTask -Part 1-
The difference between Services and AsyncTask.
AsyncTask :
AsyncTask is a class on the SDK for performing background operations and not overload the UI Thread. With this, you can load data from SQLITE, WebServices, or all short operations (a few seconds at the most). All operations will be executed in the
that will publish the results of your operation(s).
But, I need to update my UI after this operation, how can i do that ?
It is easy, AsyncTask provide also another method
that help you to update you UI.
How can I implement AsyncTask ?
For this example, I will take the case of a WebServices call.
First : Create a new Class on Android Studio.
In this example
correspond to an Array of String as Param,
the unit during the Progress and
the type for the result of you operation. It could be anything you want. I take three example.
There is four step for an AsyncTask
This previous method allow you to update UI, and prepare the AsyncTask. . For being more user-friendly, you can for example display a ProgressBar that will be dismiss on the
This method will the main “Method”. It is here you perform you long operation (few second at most), as fetching data from Internet. You have to use AsyncTask for fetching data, otherwise Android will throw an error because you are trying to do Network Operation on the main Thread.
This method will be call by the original method publishProgress. You can update a percentage for in the progressBar.
And the last method, can allow you to publish result on the UI.
You can adapte, the three type for the AsyncTask as you want.