Datasheet
17
Chapter 1: Developing Spectacular Android Applications
A thread is a process that runs separately from and simultaneously with every-
thing else that’s happening.
When would you use asynchronous processing? I’m glad you asked! You’d
use asynchronous processing for tasks that take a long time — network com-
munication (Internet), media processing, or anything else that might make
the user wait. If the user has to wait, you should use an asynchronous call
and some type of UI element to let him know that something is happening.
Failing to use an asynchronous programming model can cause users of your
application to believe that your application is buggy. Downloading the latest
Twitter messages via the Internet takes time, for example. If the network gets
slow, and you’re not using an asynchronous model, the application will lock
up, and the user will assume that something is wrong because the application
isn’t responding to her interactions. If the application doesn’t respond within
a reasonable time that the Android OS defines, Android presents an “applica-
tion not responding” (ANR) dialog box, as shown in Figure 1-2. At that time,
the user can decide to wait or to close the application.
Figure 1-2:
An ANR
dialog box.
It’s best practice to run CPU-expensive or long-running code inside another
thread, as described in the Designing for Responsiveness page on the Android
developer site (http://developer.android.com/guide/practices/
design/responsiveness.html).
Background services
If you’re a Windows user, you may already know what a service is: an applica-
tion that runs in the background and doesn’t necessarily have a UI. A classic
example is an antivirus application that usually runs in the background as a
service. Even though you don’t see it, you know that it’s running.
05_9780470770184-ch01.indd 1705_9780470770184-ch01.indd 17 11/2/10 8:51 AM11/2/10 8:51 AM