To maintain the continuous responsiveness of applications, a good habit is to remove all slow and time-consuming operations from the main thread and put them in the Child thread.
All Android application components, including activity, service, and broadcast receiver, run on the main thread. Therefore, time-consuming operations in any component will block other components, including services and visible activities.
Using Background threads is crucial to avoid the "Application No response" dialog box described in Chapter 2nd. No response defined in Android means that the activity does not respond to any input event (such as a key event) or broadcast receiver does not complete onreceive processing operations within 10 seconds.
Not only do you want to avoid this situation, but you should not even be able to encounter this situation. Background threads are used for time-consuming processing, including file operations, network searches, database interactions, and complex computing.
Create a new thread
You can use the handler class in Android and the Thread class in Java. Lang. thread to create and manage subthreads. The following framework code shows how to move an operation to a child thread:
// This method is called on the main GUI thread.
Private void mainprocessing (){
// This moves the time consuming operation to a child thread.
Thread thread = new thread (null, dobackgroundthreadprocessing, "Background ");
Thread. Start ();
}
// Runnable that executes the background processing method.
Private runnable dobackgroundthreadprocessing = new runnable (){
Public void run (){
Backgroundthreadprocessing ();
}
};
// Method which does some processing in the background.
Private void backgroundthreadprocessing (){
[... Time consuming operations...]
}
Synchronous threads for GUI operations
Whenever you use background threads in the GUI environment, it is important to synchronize the child thread and main thread before creating and modifying the graphical components.
The handler class allows you to delegate a method to the thread that creates the handler class. Using the handler class, you can use the POST method to notify the updated UI from the background thread. The following example shows the Framework Code for updating GUI threads using handler:
Hread:
// Initialize a handler on the main thread.
Private handler = new handler ();
Private void mainprocessing (){
Thread thread = new thread (null, dobackgroundthreadprocessing, "Background ");
Thread. Start ();
}
Private runnable dobackgroundthreadprocessing = new runnable (){
Public void run (){
Backgroundthreadprocessing ();
}
};
// Method which does some processing in the background.
Private void backgroundthreadprocessing (){
[... Time consuming operations...]
Handler. Post (doupdategui );
}
// Runnable that executes the update GUI method.
Private runnable doupdategui = new runnable (){
Public void run (){
Updategui ();
}
};
Private void updategui (){
[... Open a dialog or modify a GUI element...]
}
The handler class allows you to delay delegation or execute at a specific time, using the postdelayed and postattime methods.
The uithreadutilities class provides the runonuithread method to force a method to run in the same thread as a specific view, activity, or dialog box.
In program components, notification and intent are always received and processed in Gui threads. In other cases, such as explicitly interacting with an object (such as view) created in a GUI thread or displaying a message (such as toast), all these operations must be requested (invoke) to the main thread.
Move earthquakeservice to the background thread
The following code demonstrates how to move the network search and XML parsing operations in earthquakeservice to a background thread:
1. Rename the refreshearthquakes method to dorefreshearthquakes.
Private void dorefreshearthquakes (){
[... Previous refreshearthquakes method...]
}
2. Create a New refreshearthquakes method. It needs to start a thread that executes the new named dorefreshearthquakes method.
Private void refreshearthquakes (){
Thread updatethread = new thread (null, backgroundrefresh, "refresh_earthquake ");
Updatethread. Start ();
}
Private runnable backgroundrefresh = new runnable (){
Public void run (){
Dorefreshearthquakes ();
}
};