Explain in detail the basic knowledge and writing method of service service in Android

First, let's confirm what a service is?

Service is a service in the Android system. It has several characteristics: it cannot interact directly with the user, it must be explicitly started by the user or other programs, and its priority is higher. It has lower priority than the application in the foreground, but higher priority than other applications in the background, This determines that when the system destroys some unused resources due to lack of memory, the probability of it being destroyed is very small.

So, when do we need to use service? As we know, service is an application running in the background, which has lost the focus of attention for users. This is similar to when we want to see the pictures after turning on the music playback. At this time, we don't want to stop the music, so we will use service here; For another example, after we open a download link, we certainly don't want to stare until he finishes downloading, right? At this time, if we want the mobile phone to download in the background and let me see the news, we need to use the service.

Service Classification: generally, we think that services are divided into two categories: local services and remote services.

1. Local service: as the name suggests, it is a service in the same process as the current application, which has a common memory area, so it is particularly convenient and simple to share some data;

2. Remote service: it mainly involves service access between different processes. Because of Android's system security, we can't share data in a common way between different processes. Here, Android provides us with an Aidl tool. (Android interface description language) Android interface description language. We will introduce it in detail later.

Service start process context. Startservice() start process: context. Startservice() - > oncreate() - > onstart() - > service running - > context. Stopservice() - > ondestroy() - > service stop

If Service is not yet running, Android first calls onCreate () and then calls onStart (); If the service is already running, only onstart() will be called, so the OnStart method of a service may be called repeatedly. If the stopservice will be ondestroy directly, if the caller exits directly without calling stopservice, the service will always run in the background. After the caller of the service starts again, he can close the service through stopservice. Therefore, the life cycle of calling startservice is: oncreate -- > OnStart (can be called multiple times) - > ondestroy

Context. Bindservice() starts the process: context. Bindservice() - > oncreate() - > onbind() - > service running - > onunbind() - > ondestroy() - > service stop onbind() will return an ibind interface instance to the client, which allows the client to call back the service methods, such as obtaining the service instance, running status or other operations. At this time, the caller (context, such as activity) will be bound with the service. When the context exits, srevice will call onunbind - > ondestroy to exit accordingly. Therefore, the life cycle of calling bindservice is: oncreate -- > onbind (binding only once, not multiple times) - > onunbind -- > ondestory. During the opening and closing process of a service, only OnStart can be called multiple times (through multiple startservice calls), and other oncreate, onbind, onunbind and ondestory can only be called once in a life cycle.

Service life cycle:

Compared with an activity, the life cycle of a service can no longer be simpler. There are only three methods: oncreate() - > onstart() - > ondestroy().

Activity and service related methods:

If we want to use some data or access some methods in the service, we need to use the following methods:

Intent is the intent that jumps to the service, such as intent = new intent(); intent.setClass(this,MyService.class);

Steps to use service:

Step 1: we should inherit the service class and implement our own service.

If you want to access some values in the service, we usually provide an internal class that inherits binder and returns it to the service request through the onbund () method. In fact, the internal class can access the properties of the external class.

Step 2: register in Android manifest.xml, such as:

Step 3: start, bind, unbind or stop the service in the activity.

(many books say that services and users cannot interact. In fact, this statement is very incorrect. We can interact with services through activities! I think the exact statement should be that services and users cannot interact directly).

The following example provides an example of calling a service to listen to music:

Activity Code:

Class inheriting service:

Service.xml Code:

androidManifest.xml

Final rendering:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>