Android service-inherits the intentservice class

Source: Internet
Author: User

Most started services do not need to process multiple requests at the same time (this is actually a dangerous multi-threaded scenario). Therefore, using intentservice classes to implement your own services may be the best.

The intentservice class performs the following operations:

1. Create an Independent ApplicationProgramThe default worker thread of the main thread, which executes all intent processing for the onstartcommand () method;

2. Create a work queue so that only one intent can be passed to your onhandleintent () method each time, so you don't have to worry about multithreading;

3. All startup requests are processed and the service is terminated. Therefore, you do not need to call the stopself () method on your own;

4. Provide the default implementation of the onbind () method that returns NULL;

5. Provides the default onstartcommand () method and onhandleintent () method for sending intent objects to the work queue.

Therefore, you only need to implement the onhandleintent () method to complete the work provided by the customer (although you still need to provide a small constructor for the service ).

For example:

Public class hellointentservice extends intentservice {

/**
* A constructor is required, and must call the super
Intentservice (string)
* Constructor with a name for the worker thread.
*/
Public hellointentservice (){
Super ("hellointentservice ");
}

/**
* The intentservice callthis method from the default worker thread
* The intent that started the service. When this method returns, intentservice
* Stops the Service, as appropriate.
*/
@ Override
Protected void onhandleintent (intent ){
// 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 ){
}
}
}
}
}

The above is all you have done: the implementation of a constructor and an onhandleintent () method.

If you decide to override other callback methods, such as oncreate (), onstartcommand (), or ondestroy (), make sure to call the implementation of the parent class, so that the intentservice object can properly process the activity of the working thread.

For example, the onstartcommand () method must return the default implementation (the default implementation gets the intent and delivers it to the onhandleintent () method ):

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

Except for the onhandleintent () method, the only method that does not need to be called is the onbind () method (but if your service allows binding, you must implement this method ).

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.