Android Service (one)

Source: Internet
Author: User

First, what is a service? When do we need to use the service?

Service is the services in the Android system, it has a few features: it can not interact directly with the user, it must be explicitly launched by the user or other programs, it has a higher priority, it is lower than the application in the foreground, but higher priority than other applications in the background, This determines that when the system destroys certain underutilized resources because of lack of memory, it is less likely to be destroyed.

Service is an app that runs in the background and loses focus for the user. After we opened the music, we wanted to look at the pictures, we didn't want the music to stop, we used the service here, for example, after we opened a download link, we certainly didn't want to stare and wait for him to finish downloading and then do something else, right? At this time if we want to download the phone in the background, while you can let me go to see the news, it is necessary to use the service.

General:Service used to process background tasks

Second, service classification
In general, we believe that service is divided into two categories, Local Service and remote service.
Local service as the name implies, that is, and the current application in the same process of service, each other has a common memory area, so for some data sharing is particularly convenient and simple;
Remote service: Mainly involves service access between different processes. Because of the security of Android system, we cannot share data in a common way between different processes. Here Android provides us with a aidl tool, (Android Interface Description Language) Android Interface Description Language. We will introduce them in detail in the rear.

Three, service two kinds of starting mode and life cycle

There are two ways to start a service:

StartService () in unbounded mode;

Bindservice () in bounded mode;

StartService ()

Usage Scenarios: services opened via StartService. Once the service is open, there is no relationship between the service and the caller who opened it.
Callers are not allowed to access the methods inside the service. If the caller is reclaimed by the system or the OnDestroy method is called, the service will continue to exist

Life cycle:

Context.startservice (), OnCreate (), OnStart (), Service running, Context.stopservice (), ondes Troy (), Service stop

If the service is not running, Android calls OnCreate () first and then calls OnStart ();

If the service is already running, only OnStart () is called, so a service's OnStart method may be called repeatedly.

If the StopService is directly OnDestroy, the service will run in the background if the caller exits directly (OnPause) without calling StopService. The service can be shut down by StopService after the caller has started up again.

The service can be stopped by the Service.stopself () method or the Service.stopselfresult () method, as long as the StopService () method is called to stop the service, regardless of how many times the startup service method is invoked.

So the life cycle of calling StartService is: onCreate---OnStart (can be called multiple times) --OnDestroy

Bindservice ()

usage Scenario: A service opened through Bindservice, and the service is open, and there is still a connection between the caller and the service.
Once the caller has hung up, the service will follow.

Life cycle:

Context.bindservice (), OnCreate (), Onbind (), Service running, Onunbind (), OnDestroy () Service stop
Onbind () returns a Ibind interface instance to the client, Ibind a method that allows the client to callback the service, such as getting an instance of the services, running state, or other operations. This time the caller (context, for example, activity) is bound together with the service, the context exits, Srevice will call Onunbind->ondestroy exit accordingly.

When its caller exits (OnPause) or calls Unbindservice (connection); You can stop the service (you can not use it).

So the lifetime of the call Bindservice is: onCreate---Onbind (one time, not multiple bindings) --onunbind--and Ondestory

Only OnStart can be called multiple times (through multiple StartService calls) during each turn-off of the service, and the other oncreate,onbind,onunbind,ondestory can only be called once in a life cycle.

Android Service (one)

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.