Android service details 1: Overview

Source: Internet
Author: User

A service is an application component that has no interface and can run on the background for a long time. other application components can start a service and run on the background, even if the user switches to another application, it will continue to run. in addition, a component can be bound to a service for interaction, even if the interaction is inter-process communication. for example, an aservice may process network things, play music, execute file I/O, or interact with a content provider, all of which are performed in the background.

A service can be expressed in two forms:

  • Started

    A service is in the "started" state when startservice () is called by an application component (such as an activity ). after running, the service can run indefinitely in the background, even if its components are destroyed. generally, a startedservice executes a single operation and does not return the result to the caller. for example, it may download or upload an object over the network. after the operation is complete, the Service stops itself.

  • Bound

    A service is in the "Bound" status when an application component calls bindservice. A boundservice provides a client-server interface to allow components to interact with the service, send requests, obtain results, and even perform cross-process interaction. A boundservice runs only when it is bound to a component of another application. multiple application components can be bound to a single service at the same time. However, when all the competing components are no longer bound, the service is destroyed.

Although this document separates the two services, your service can work in these two ways. -It can be started (running indefinitely) and can be bound. the only simple question is whether you have implemented a pair of callback Methods: onstartcommand () allows the component to start it and onbind () allows binding.

Whether the application is started or bound to a service or both, any application component can use the service (even from another application ), like any component, you can use activity-start it with an intent. however, you can declare the service as private in the manifest file and prevent other applications from accessing it. this will explain in detail how to declare the service in the manifest file.

Note: a service is running in the main thread of its process. The service does not create its own thread or run in a separate process (unless otherwise specified ). this indicates that if your service wants to do some CPU-consuming or blocking operations (such as MP3 playback or network communication ), you must create a new thread in the service to do the work. by using a separate thread, you will reduce "Application No response" (ANR) errors and the main thread of the application can keep the activity fast response to user operations.

Basic

You should useServiceOr thread?

OneServiceIs a simple component that can still run on the background when the user does not interact with your application. Therefore, you only need to createService.

If the job you want to execute is not in the main thread, but only when the user interacts with your application, you may create a new thread instead ofServiceFor example, if you want to play some musicActivityYou mustOncreate ()Create a thread inOnstart ()Run this thread, and thenOnstop ()To stop it. You can also consider usingAsynctaskOrHandlerthreadTo replace the traditional Thread class.

Remember, if you useServiceBy default, it will be fixed to the main thread of your application, so you should create a thread in it to execute time-consuming or blocking operations.

To create a service, you must create a subclass of a service class (or an existing subclass. in your implementation, you should overwrite some callback methods for handling key aspects of the service life cycle and provide a mechanism for binding components to the Service (if needed ). the most important callback method you should override is:

  • Onstartcommand ()

    The system calls this method when other components, such as activity, call startservice () to request service startup. once this method is executed, the service starts and runs in the background for a long time. if you implement it, you need to stop the service when it completes the task by calling stopself () or stopservice (). (If you only want to provide binding, you do not need to implement this method ).

  • Onbind ()

    The system calls this method when the component calls bindservice () and wants to bind it to the service (for example, to execute inter-process communication. in your implementation, you must provide an ibinder for the client to communicate with the service. You must always implement this method, but if you do not allow binding, then you should return null.

  • Oncreate ()

    The system executes this method when the service is created for the first time to execute initialization only once (before calling the method such as onstartcommand () or onbind ). if the service is already running, this method will not be called.

  • Ondestroy ()

    This method is called when the service is no longer in use and is to be destroyed. your service should release resources in this method, such as threads, registered listeners, and receivers. this is the last call received by the Service.

If a component starts a service by calling startservice (), the Service will continue to run until it passes stopself () stop yourself or other components call stopservice () to stop it.

If a component calls bindservice () to create a service (onstartcommand () is not called), the service is only running during the binding period. once the service is unbound from all clients, the system will kill it.

The Android system forces the stop of a service only when the actvity quota with few memory and user focus is placed on the resource. if the service is bound to an activity with a user focus, it is difficult to be killed. if the service is declared as running on the foreground (as discussed later ), it will never be killed unless, if the service is started and runs for a long time, then the system will reduce its location in the backend task timeout list, and then this will become highly vulnerable objects to be killed-if your service is started, therefore, it must be designed to handle the operation that is restarted by the system. if the system kills your service, it will immediately restart it when the resource is re-available (but it depends on the returned value in onstartcommand ). for more information about when the system will kill a service, see: Android
Detailed description of processes and threads.



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.