Android service-inherits the service class

Source: Internet
Author: User

It is easy to inherit the intentservice class to implement a service of the started type, but if you need the service to execute multiple threads (instead of processing the startup request through the work queue ), then you must inherit the service class to process each intent.

For ease of comparison, in the following example, the implementation code of the service class executes the same work as the example in the previous section "inheriting the intentservice class", that is, for each startup request, it uses a worker thread to execute the work and processes only one request at a time.

Public class helloservice extends Service {
Private low.mserviceloaders;
Private servicehandler mservicehandler;

// Handler that reads es messages from the thread
Private final class servicehandler extends handler {
Public servicehandler (low.logoff ){
Super (logoff );
}
@ Override
Public void handlemessage (Message MSG ){
// Normally we wowould do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
Long endtime = system. currenttimemillis () + 5*1000;
While (system. currenttimemillis () <endtime ){
Synchronized (this ){
Try {
Wait (endtime-system. currenttimemillis ());
} Catch (exception e ){
}
}
}
// Stop the service using the startid, so that we don't stop
// The service in the middle of handling another job
Stopself (msg. arg1 );
}
}

@ Override
Public void oncreate (){
// Start up the thread running the service. Note that we create
// Separate thread because the service normally runs in the process's
// Main thread, which we don't want to block. We also make it
// Background priority so CPU-intensive work will not disrupt our UI.
Handlerthread thread = new handlerthread ("servicestartarguments ",
Process. thread_priority_background );
Thread. Start ();

// Get the handlerthread's logoff and use it for our handler
Mserviceloaders = thread. getlooper ();
Mservicehandler = new servicehandler (mserviceloader );
}

@ Override
Public int onstartcommand (intent, int flags, int startid ){
Toast. maketext (this, "service starting", Toast. length_short). Show ();

// For each start request, send a message to start a job and deliver
// Start ID so we know which request we're stopping when we finish the job
Message MSG = mservicehandler. obtainmessage ();
MSG. arg1 = startid;
Mservicehandler. sendmessage (MSG );

// If we get killed, after returning from here, restart
Return start_sticky;
}

@ Override
Public ibinder onbind (intent ){
// We don't provide binding, so return null
Return NULL;
}

@ Override
Public void ondestroy (){
Toast. maketext (this, "service done", Toast. length_short). Show ();
}
}

As you can see, it does more work than inheriting the intentservice class.

However, because you process the call of each onstartcommand () method, You can execute multiple requests at the same time. This is not done in the above example, but if you want to do so, you can create a new thread for each request, and run them immediately (instead of waiting for the previous request to complete ).

Note:The onstartcommand () method must return an integer that describes how the system continues the service value in the system's killing event (although you can modify this value, however, intentservice provides the default implementation for you ). The value returned from the onstartcommand () method must be the following constant:

Start_not_sticky

If the system kills the service after the onstartcommand () method returns, the service will not be re-created until a new intent object is received. This is the safest option to avoid running your service when you don't need it.

Start_sticky

If the system kills this service after onstartcommand () is returned, the system will re-create this service and call the onstartcommand () method, but it will not re-pass the last intent object, the system uses a null intent object to call the onstartcommand () method unless some intent objects are waiting to start the service. This applies to media players (or similar services) that do not execute commands. It only runs indefinitely and waits for the arrival of work.

Start_redeliver_intent

If the system returns the onstartcommand () method, the system creates the service again and calls the onstartcommand () method with the intent object that is sent to the service. Intent objects in any wait state are sent in sequence. This applies to services that should immediately restore ongoing work, such as downloading files.

Related Article

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.