"Android Essentials"-service components using the detailed

Source: Internet
Author: User

Service is one of the four components of Android, which differs from activity in that it has been running in the background without a foreground interface. Once the service is started, he has the same life cycle as the activity.


first, create service, define a subclass that inherits the service

A series of life cycle methods are defined in the service, as follows:

    • ibinder Onbind (Intent Intent): This method is a method that the service must implement. The method returns an IBinder object that the application can use to communicate with the service component.
    • void onCreate (): This method is called when the service is first created.
    • void ondestory (): The method is called before the service is closed.
    • void Onstartcommand () : The early version of the method is the OnStart () method, which calls Onstartcommand () every time the StartService (Intent Intent) method service is called.
    • boolean onunbind (Intent Intent) : The service executes this method when all clients bound by the service are disconnected.
    • void onrebind (intent Intent ): When Onunbind is put back to true, after the client calls Unbindservie, the Bindservice method is executed when the onrebind is called.

Second, Configure the service in the Androidmanifest.xml file;
        <service            android:name= "Org.iti.tailyou.servicedemo.MService"            android:exported= "false" >            < intent-filter>                <action android:name= "Org.iti.tailyou.MService" >                </action>            </ Intent-filter>        </service>
the <INTENT-FILTER> can be set when configured, and the action to start the service is defined in the filter.
    1. If the service is configured <intent-filter> is set, and by default the components of other applications can start the service, which can be set by android:exported= "false" to prohibit other application components from starting the service.
    2. If the service is configured without set <intent-filter>, by default, the components of other applications may not start the service .

third, the start of service mode
    • Through the context of the StartService () method: Starting the service through this method, the visitor is not associated with the service, even if the component that started the service exits, the service is still running.
    • Through the Bindservice () method of the context: This method starts the service, the visitor binds to the service, and once the visitor exits, the service terminates.

Iv. service life cycledepending on how the service is started, the service life cycle also differs:
    1. StartService starts, executes the OnCreate, Onstartcommand method, the service itself stops or the caller stops, and executes the Ondestory method. Each time the StartService method is called, the Onstartcommand method is executed once.
    2. Bindservice start, execute OnCreate, Onbind method, call Unbindservice or caller exit, execute Onunbind, ondestory method, no matter how many times the client calls Bindservice method, are only executed once Onbind method.
    3. Bind to a service that is already started, call the Unbindservice method , execute the Onunbind method only, call the Bindservice method again, and execute the Onrebind method.

v. The difference between service and thread
    • Thread: The smallest unit of program execution, which is the smallest unit of CPU scheduling, can be performed with thread to perform some asynchronous operations.
    • Service:service is a mechanism in Android that runs in the background in Android, and as with activity, it runs in the main line of the program. It is not possible to take time-consuming actions in the service, and it is recommended to start a new thread in the service to execute if time-consuming tasks are to be performed in the service.
    • Why do I need a service instead of using thread directly? 1, unable to control the same thread in different activity, 2, start the thread in the activity, the activity exits, the thread is still running, but the activity has no reference to the thread, no longer control the thread.

Liu, Intentservice1) Why introduce Intentservice?
    1. Starting a service does not start a new process, it is in the same process as the application in which it resides;
    2. The service is not a new thread, it runs in the main thread of the program, so it is not possible to process time-consuming operations directly in the service.
2) Intentservice Implementation mechanismIntentservice uses queues to manage intent requests from clients, and Intentservice joins the intent queue whenever a client initiates intentservice through intent requests. A new worker thread is then opened to process the intent in the queue in turn. 3) Characteristics of Intentservice
    1. Intentservice will create a separate worker thread to handle all Intent request;
    2. After all intent requests are processed, Intentservice will stop automatically;
    3. Inheriting Intentservice simply overrides the Onhandleintent () method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Android Essentials"-service components using the detailed

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.