Android-Service Components

Source: Internet
Author: User

Android-Service Components

 

A Service is one of the four major components of android. It does not have a UI and can be used in the background for a long time. Other components can start a Service. After the service is started, it will continue to run in the background, regardless of whether the user switches to other applications. In addition, other components can be bound to a service, interact with the service, and even perform cross-process operations (IPC ). For example, the Service can process network requests, play music, execute file I/o, or interact with the content provider in the background.

There are two forms of service:
Started
When other components start by calling startService (), the service is "Started ". Once started, the service will run infinitely in the background, even if its components have been destroyed. Generally, Started service only performs a single operation and does not return the returned value to the caller. For example, it can be downloaded or uploaded over the Internet. When the operation ends, the service must manually stop itself.

Bound
When other components are bound to the service by calling bindService (), the service is bound. Bound service provides client/server interfaces that allow components to interact with the service, send requests, obtain results, and even perform cross-process operations. Bound service only exists when other components are bound to it. Multiple components can be bound to one service at the same time. However, when all components are unbound at the same time, the service will be destroyed.

Although this document discusses the two types of services separately, your service can support both types at the same time. The service can be started (unlimited run) and can be bound at the same time. This only depends on whether you have implemented the following callback: onStartCommand () allows other components to start a service, and onBind () allows binding a service.

Whether your service is started or bound, other components of the application can use this service (or even other applications ). Just as other components can use an Activity, use Intent to start it. However, you can declare the service as private in the manifest file, which prevents other applications from accessing it.

 

For more information, see the section declare service in the manifest file (http://developer.android.com/guide/components/services.html#Declaring.

Note: The service runs in the main thread of the host process. The service does not create its own thread and does not run in a separate process unless you specify it. This means that if your service requires CPU or blocking operations (such as playing MP3 or network requests ), you need to manually create a new thread in the service for this operation. Using a separate thread reduces ANR risks and enables the main thread to focus on UI operations.

Service Basics

To create a service, you must create a subclass of the Service (or Service subclass. In your implementation, You need to rewrite some callback methods to process some key lifecycles of the service and provide a mechanism for binding components to the service, if appropriate.

The most important callback to be rewritten is:

OnStartCommand ()

When other components start the service by calling startService (), the system calls this method. Once this method is executed, the service starts up and runs infinitely in the background. If you overwrite this method, you must call stopSelf () or stopService () to destroy the service after the service is finished. (This method is not required if you only want to provide binding)

OnBind ()
When other components bindService () is called to bind to service (such as RPC), the system will call this method. To implement this method, you must return an IBinder to provide an interface for the client to interact with the service. This method is generally implemented, but if you do not allow binding, you can return null.

OnCreate ()
When the service is started for the first time, the system will call this method and only execute it once (before onStartCommand () or onBind () is called ). If the service is already running, this method will not be called.

OnDestroy ()
This method is called when the service is no longer used or destroyed. Your service needs to implement this method to clean up resources, such as threads, listeners, and broadcast receivers. This is the last callback received by the service.

If a component starts a service through startService (), the service will continue to run until it calls stopSelf () or other components call stopService () to stop it.

If a component starts a service (onStartCommand () through bindService (), the service will only survive the component bound to it. Once the service is unbound from all clients, it will be terminated by the system.

The Android system will force the service to be killed only when the system memory is insufficient. The system must be able to provide system resources to activity with user focus.
If the service is bound to an activity with the user focus, it is likely not to be killed.
If the service is declared as running on the foreground, it will not be killed.
In other cases, if the service is started and runs for a long time, the system places it in the back-to-back position of the background task list over time, it becomes easy to be killed.
If your service is started, you must enable it to handle the restart of the system elegantly.
If the system kills your service, once the resource is available, the system will immediately restart the service to be killed (Of course, this also depends on the onStartCommand () return value ).
Want to know

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.