In Part-7,
we demostrated how a background thread could update the user interface
using
a Handler. An simpler and easier way to
acheive the same
is to use the Android framework class android.os.AsyncTask.
The reason AsyncTask is simpler and easier
is because it
automatically creates the background processing thread and manages the
UI updates.
In order to use AsyncTask, we need to
extend the android.os.AsyncTask class and
specify 3 parameter types. The 3 parameter types indicate the type
of objects that will be passed to 3 callback methods we need to
override
in the extended subclass. The following are the 3 methods that need
to be overriden:
doInBackground() :: method that
will be
executed in the background thread. This is where we place the code that
will
be long running and time consuming
onProgressUpdate() :: callback
method
that is invoked in response to the invocation of the method publishProgress(). The method publishProgress() is typically invoked
from
within the method doInBackground() to
update progress of the long running task. This is where we place
the code to update the user interface
onPostExecute() :: callback
method that is
invoked when the method doInBackground()
completes
We will now demonstrate this concept of using AsyncTask
by creating a new Android application named DroidStockTicker2.
This application is essentially the same as DroidStockTicker
except that it uses AsyncTask.
Without further ado, lets get started. We will not go step-by-step
to show the various
screens since we already did that in Part-2 for the DroidTipCalculator
application.
The contents of the dimens.xml
file to look like the one shown in the
listing 8.1 below:
The contents of the strings.xml
file to look like the one shown in the
listing 8.2 below:
The contents of the main activity layout in file activity_stock_ticker.xml
will look like the one shown in the listing 8.3 below:
The contents of each list item layout in file list_item_row.xml
will look like the one shown in the listing 8.4 below:
The Java class TickSignal is shown
in the listing 8.5 below:
The Java class
StockTicker is shown
in the listing 8.6 below:
The Java class
TickerArrayAdapter is shown
in the listing 8.7 below:
Finally, we have the java source file corresponding to the main
stock ticker Activity called
StockTickerActivity.java.
The contents of the java source file StockTickerActivity.java
will look like the one shown in the listing 8.8 below:
From the code Listing 8.8 above, the following are some of the
interesting details:
Define an inner class TickerUpdateTask
that sub-classes the Android framework class android.os.AsyncTask.
We override doInBackground() and
implement
code that will wake-up every 10 seconds, update the stock price for
each symbol in the
ArrayList of StockTicker
objects.
After the stock prices are updated, we invoke publishProgress()
to trigger a call to onProgressUpdate().
We override onProgressUpdate() to update the
user
interface with the latest stock prices
In the OnCreate() method, we create
an ArrayList of StockTicker
objects corresponding to the symbols and open prices from strings.xml
Create an instance of TickerArrayAdapter
object
with the ArrayList of StockTicker
objects as the data set for the ListView
Create an instance of TickerUpdateTask
object
and invoke the execute()
method
The following figure 8.1 shows how the parameter types of AsyncTask
relate to the overriden method parameter types:
Figure-8.1
We are now ready to test our DroidStockTicker2
application on the virtual Android device we created in
Part-1.
We will create a Run Configuration for DroidStockTicker2
as we did in Part-2
for DroidTipCalculator.
Once the run configuration for DroidStockTicker2
is ready, we will Run
the application and the application will come to life as shown in the
following figure 8.2 below:
Figure-8.2
Wait about 10 seconds and the device screen will be refreshed as
shown in the
following figure 8.3 below: