As a concrete example of a bad implementation and what happens when a long running operation is done on the UI Thread, I want to refer to one of my previous tutorials: Creating A Simple RSS Application in Android. Well, that application is working fine, and it does what it is supposed to do – parse an XML feed and display the headlines in a ListView. The “vulnerability” of that application is that the network access is done directly on the UI Thread which makes the application to “freeze” while the XML feed is downloaded .
When I created that tutorial I wanted to make it as simple as possible without dealing with more advanced topics like asynchronous tasks. The intent of tutorial was to show the working process with feeds on a high level. But I promise you, by the end of this article you will be able to fix it and have a Cool Rss App that runs smoothly! :)
To provide a good user experience all long running operations in an Android application should run asynchronously. To achieve this we will be using the AsyncTask class.
What does AsyncTask do?
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 extend it and override at least the doInBackground() method.
The most common methods you will need to implement are these:
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. Referring to our Simple RSS Aplication, you would put here the code that downloads the XML feed and does the parsing. 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 AsyncTaskProgress
– if you have any updates, passed to onProgressUpdate()Result
– the output. what returns doInBackground()
new DownloadTast().execute(url1, url2, urln);
Code Example
This is a simple skeleton of an AsyncTask implementation.public class AsyncTaskTestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); //Starting the task. Pass an url as the parameter. } // The definition of our task class private class PostTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { super .onPreExecute(); displayProgressBar( "Downloading..." ); } @Override protected String doInBackground(String... params) { String url=params[ 0 ]; // Dummy code for ( int i = 0 ; i <= 100 ; i += 5 ) { try { Thread.sleep( 50 ); } catch (InterruptedException e) { e.printStackTrace(); } publishProgress(i); } return "All Done!" ; } @Override protected void onProgressUpdate(Integer... values) { super .onProgressUpdate(values); updateProgressBar(values[ 0 ]); } @Override protected void onPostExecute(String result) { super .onPostExecute(result); dismissProgressBar(); } } } |
Comments
Post a Comment