App->service->service Start Arguments Controller
This example is the last example of Apidemos about service, and the main purpose of this example is to describe how to pass parameters to a service. The previous example ignores an important problem: the service's Onstartcommand or OnStart (before version 2.1) is executed using the same thread as the Android component (usually the activity) that calls it. For activity, this thread is usually UI thread, and the previous service example provides very simple services that don't have much impact on UI performance, but if you use more time-consuming operations in your service, if you have network access, database queries, If you use UI thread to run, you can significantly reduce the responsiveness of the UI and even the ANR (application not Response) dialog box, so a new thread is usually created in the service to handle requests from the client.
Android.os's handler, Handlerthread, Loop, and message are commonly used in service, where handler introduction can be found in the Android system handler usage profile.
Each thread in Android can have a message queue, but with the exception of UI thread, thread does not have a line queue by default, and to create a thread queue for one, you can see the following code:
Class Looperthread extends Thread {public
Handler Mhandler;
public void Run () {
looper.prepare ();
Mhandler = new Handler () {public
void Handlemessage (Message msg) {
//process incoming messages here
}
};
Looper.loop ();
}
Looper.prepare () is used to create a message Queue, Looper.loop () processes messages until the loop stops. The handler that is created in thread will be associated with the thread's message queue. The handler handlemessage is used to process messages with the type message class.
Handlerthread is derived from thread and is used to facilitate the creation of a thread with Looper. This example is the use of handlerthread.
private volatile looper mservicelooper;
private volatile servicehandler Mservicehandler;
Private Final class Servicehandler extends Handler {public
Servicehandler (Looper looper) {
super (Looper);
}
@Override public
void handlemessage (Message msg) {
...
}
...
Handlerthread thread = new Handlerthread ("Servicestartarguments",
process.thread_priority_background);
Thread.Start ();
Mservicelooper = Thread.getlooper ();
Mservicehandler = new Servicehandler (mservicelooper);
The Servicestartarguments service uses a newly created thread to process messages from the client, this thread is not UI thread, and creates a thread with Looper using Handlerthread. and the specific message processing is handled by Servicehandler's handlemessage.
The message class defines messages that can contain a description of the message description, type what, default band arg1,arg2, obj three parameters can be used directly. A obtain () static function is provided to construct a new message object.
Servicestartarguments.controller is the client of the Servicestartarguments service. It defines four buttons that pass parameters to the service through the intent Extra:
StartService (New Intent (Controller.this,
servicestartarguments.class)
. Putextra ("name", "one");
StartService (New Intent (Controller.this,
servicestartarguments.class)
. Putextra ("name", "two"));
StartService (New Intent (Controller.this,
servicestartarguments.class)
. Putextra ("name", "Three")
. Putextra ("Redeliver", true);
StartService (New Intent (Controller.this,
servicestartarguments.class)
. Putextra ("name", "failure")
. Putextra ("fail", true);