Using Worker Threads

When we use a handler like the one in the previous section, the code is still executed on the main thread. Each call to handleMessage() still should return within the time stipulations of the main thread (in other words, each message invocation should complete in less than five seconds to avoid Android Not Responding). If your goal is to extend that time of execution further, you will need to start a separate thread, keep the thread running until it finishes the work, and allow for that subthread to report back to the main activity, which is running on the main thread. This type of a subthread is often called a worker thread.

It is a no-brainer to start a separate thread while responding to a menu item. However, the clever ...

Get Pro Android 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.