Android Apidemos Sample Resolution (44)

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.