Android Services (background service)

Source: Internet
Author: User

First, Introduction

A service is a long-running application component that can be executed in the background, and it does not provide a user interface. Another application component can start a service, and it still runs in the background even if the user switches to another application. In addition, components can be bound to a service to interact with it, or even to perform interprocess communication (IPC). For example, services can process network transactions from the background, play music, perform file I/O, or interact with content providers.

These are three different types of services:

Scheduled (planned service) available after---Android 5.0
When APIs such as Jobscheduler are introduced in Android 5.0 (API level 21), a service is scheduled to start the service. You can use Jobscheduler by registering jobs and specifying their network and time requirements. The system then gracefully arranges the execution of the job at the appropriate time. Jobscheduler provides many ways to define service execution conditions.
Note: If your app is targeting Android 5.0 (API level), Google recommends that you use Jobscheduler to perform a background service. For more information about using this class, see the Jobscheduler reference documentation.

Started (start of service)
When an application component (such as an activity) calls StartService (), the service starts. Once started, the service can run indefinitely in the background, even if the component that started it is destroyed. Typically, the start service performs a single operation and does not return the results to the caller. For example, it can download or upload files over the network. After the operation is complete, the service should stop.

Bound (bound service)

When the application's build calls Bindservice (), this component binds to the service. A bound service provides a Client-server interface that allows components to interact with the service, send requests, receive results, and even interact through interprocess communication (IPC). As long as another application component is bound, the bound service runs. Multiple components can bind to the service at the same time, but when all components are unbound, the service is destroyed.

Although this article usually discusses the services that are started and bound separately, your service can work in two ways-it can start (run indefinitely) and allow bindings. This is only if you have implemented some callback methods: Onstartcommand () allows the component to start it and uses Onbind () to allow binding.

Any application component can use the service (even if it is a separate application) or any component can use the activity (by starting it with intent), whether your application is started, binding, or both. However, you can declare a service as private in the manifest file and block access to other applications. The specific setup method is described below.

Note: The service runs in the main thread, does not create its own thread, and does not run in a separate process unless specified otherwise. If your service performs any CPU-intensive work or blocking operations, such as MP3 playback or networking, you should create a new thread in the service to complete the work. By using a separate thread, you can reduce the risk of application ANR errors, and the main thread of the application can remain dedicated to interacting with your active users.

Second, API overview

Important Callback methods:

onStartCommand()

When another component requests that the service be started, the system calls this method by calling StartService (). When this method is executed, the service starts and can run indefinitely in the background. If you implement this feature, you need to stop its work by calling Stopself () or StopService (). If you only want to provide a binding, you do not need to implement this method.

onBind()

This method is called by the system by calling Bindservice () when another component wants to bind to the service (for example, execute RPC). When implementing this method, you must provide a client interface that communicates with the service by returning IBinder. You must always execute this method; However, if you do not want to allow the binding, you should return NULL.
OnCreate () This method is called by the system to perform a one-time setup process when the service is initially created (before calling Onstartcommand () or Onbind ()). This method is not called if the service is already running.
OnDestroy () This method is called by the system when the service is no longer in use and is destroyed. Your service should implement this to clean up any resources, such as threads, registered listeners or recipients. This is the last call received by the service.

If a component starts a service by calling StartService () (called Onstartcommand ()), the service will continue to run until it stops its own stopself () or another component by calling StopService () to stop it.

If a component calls Bindservice () to create a service, and Onstartcommand () is not called, the service can be run as long as the component is bound. After the service is unbound from all its clients, the system destroys it.

The Android system forces the service to stop when memory is low and must restore system resources for activity with user focus. If the service is bound to activity with user focus, it is unlikely to be killed, and if the service is declared to be running in the foreground, it will rarely be killed. If the service starts and runs for a long time, the system will reduce its position in the background task list over time and the service becomes very easy to kill-if your service is started, you must design it to gracefully handle rebooting the system. If the system kills your service, it will restart as soon as the resource is available, but it also depends on the value returned from Onstartcommand ().

Android Services (background service)

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.