Android Tutorial

(avery) #1
Android Tutorial 371

For this example, an anonymous Thread object will do. We create it
and call its start() method immediately. However, now that the
code runs on a separate thread, the user interface updates must be
posted back to the main thread. This is done by using a Handler
object on the main thread and creating Runnable objects that
execute to call setText() on the TextView widget named status.


The rest of the code remains the same as in the previous examples.
Executing both the parsing code and the networking code on a
separate thread allows the user interface to continue to behave in a
responsive fashion while the network and parsing operations are
done behind the scenes, resulting in a smooth and friendly user
experience. This also allows for handling of interim actions by the
user, such as canceling the transfer


You can Accessing the Internet (HTTP) 295 accomplish this by
implementing the Thread to listen for certain events and check for
certain flags.


Displaying Images from a Network Resource


Now that we have covered how you can use a separate thread to
parse XML, let’s take our example a bit deeper and talk about
working with non-primitive data types.


Continuing with the previous example of parsing for image
locations from a flickr feed, let’s display some images from the
feed. The following example reads the image data and displays it
on the screen, demonstrating another way you can use network
resources:

Free download pdf