The difference between Android note Intentservice and service

Source: Internet
Author: User

Service in the official API the description is

a Service is an application component representing either A application ' s desire to perform A longer-running operation W Hile not interacting with the user or to supply functionality for other applications to use. Each service class must has a Corresponding < Service>  declaration in it package ' S

note that services, like other application objects, run in the main thread of their hosting process. This means, if your service was going to does any CPU intensive (such as MP3 playback) or blocking (such as networking) Operations, it should spawn its own the thread in the which to do the work. More information on this can is found in processes and Threads. The 

mainly used for background services when the application is hung into the background, in order to ensure that some components of the application can still work and introduce the concept of service, then it is emphasized that the service is not a separate process, not a separate thread, It is dependent on the main thread of the application, and in many cases it is not recommended to write time-consuming logic and operations in the service, otherwise it will cause ANR. in this case, the introduction of Intentservice,intentservice is inherited service, then it contains all the service features, of course, also contains the service life cycle, So unlike the service, Intentservice, when performing the OnCreate operation, opens a thread inside to perform your time-consuming operation.

The service provides a method Onstartcommand this method when the service starts or is called.

Onstartcommand (Intent Intent, int flags, int startid)

Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.

Intentservice: Asynchronous Processing Service, open a new thread : Handlerthread sends a message in the thread and then accepts that processing is complete, cleans up the threads, and shuts down the service.

Intentservice has the following features:

(1) It creates a separate worker thread to handle all the intents that pass through Onstartcommand () to the service.

(2) Create a task queue to send intent to onhandleintent ().

(3) Do not need to call STOPSELFT () actively to end the service. Because, after all intent are processed, the system automatically shuts down the service.

(4) The default implementation of Onbind () returns null

(5) The purpose of the default implementation of Onstartcommand () is to insert intent into the work queue

The difference between Android note Intentservice and service

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.