AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
In order to use the AsyncTask class, you must extendit and override at least the doInBackground()method.
AsyncTask have 4 methods
1) onPreExecute()
2) doInBackground(Params…)
3) onProgressUpdate()
4) onPostExecute(Result)
1. onPreExecute() – called on the UI thread before the thread starts running. This method is usually used to setup the task, for example by displaying a progress bar.
2. doInBackground(Params…) – this is the method that runs on the background thread. In this method you should put all the code you want the application to perform in background. The doInBackground() is called immediately after onPreExecute(). When it finishes, it sends the result to the onPostExecute().
3. onProgressUpdate() - called when you invoke publishProgress() in the doInBackground().
4. onPostExecute(Result) – called on the UI thread after the background thread finishes. It takes as parameter the result received from doInBackground().
AsyncTask is a generic class, it uses 3 types:AsyncTask<Params, Progress, Result>.
Params – the input. what you pass to the AsyncTask
Progress – if you have any updates, passed to onProgressUpdate()
Result – the output. what returns doInBackground()
No comments:
Post a Comment