Explain service in Android

Service Introduction:

Service is designed to perform long-running operations in the background. Android allows services to run in the background, even after the end of the activity, so relatively speaking, services have higher priority than activities.

Create service:

To create a basic service, you need to do the following: 1) create a Java class and let it inherit the service; 2) override oncreate() and onbind() methods

Where oncreate () is the method executed when the service is created, and onbind () is the method executed when the service is bound.

After a new service is created, it must also be configured in the androidmanifest.xml file, and a service tag needs to be included in the application node.

Of course, if you want your customized service to be used only by the application written by yourself, you can also add:

android:permission="exam02.chenqian.com.servicedemo"

Let the service perform specific tasks:

If you want the service to perform a specific task, you can copy the onstartcommand() method of the service. Note that the onstart() method before api15 is not recommended. The onstartcommand() method is executed after the service oncreate().

Start and stop service:

Explicitly start a service:

To facilitate observation, we can add log. I ("servicestate", "-------------- > is running") to the onstartcommand() method in the custom service class created earlier;

When we call and run from mainactivity, we can observe the output in logcat: I / servicestate: is running. Of course, we can also stop a service. In order to observe the effect more clearly, we can copy the ondestroy() method in exampleservice class:

You can stop a service in mainactivity in the following ways:

Display and stop a service: note that when writing here, a service is replaced and the customized service is positioned as myservice. It is no longer the previous exampleservice, but you agree to continue writing according to your previous methods. After all, the methods are the same; -)

Note that service calls cannot be nested, so no matter how many times a service is called, a call to stop stopservice () will terminate the running service it matches.

Because the service has a high priority, it is usually not terminated by the runtime. Therefore, self termination can be used to avoid running the service in the background consuming system resources. The specific method is to add stopself() to onstartcommand() method; However, it should be noted that stopself () here does not directly terminate the service, but stops the service after all functions or requests of the service are executed, rather than waiting for the system to recycle. Stopping will call ondestroy () to destroy the service.

Bind service to activity:

When a service is called in an activity, it will not be destroyed with the destruction of the activity, but may still continue to run in the background and continue to occupy the resources of the system. Therefore, if the related services are automatically stopped when the activity is destroyed, the resource occupation of the system will be greatly saved, We can bind activity to service in the following ways:

XML layout file: four buttons are implemented in the layout file to start service, stop service, bind service and unbind service respectively. Are you clear: -)

Myservice class:

/ * we know that the Android system isolates each application in a corresponding independent "sandbox" for security protection and overall stability, so our customized service actually runs in

In user space, there are many services that need to reference the services of the system. How can they communicate in user space and system space, which requires binder,

Binder is a way to realize the communication between different processes in the Android system. Binder itself means adhesive. Binder can bond the four components in the Android system. Therefore, below, we create a new mybinder internal class in the myservice class and let it inherit the binder class to obtain myservice, You should know why we want to create a new member variable of my binder ^ ^ ^, You can also see the application of relevant examples in the mainactivity below,

for example

Mainactivity class:

AndroidManifest.xml

About the future:

1. I don't think binder's explanation is very clear. I'll study it further and complete it later

2. There will be time to write a simple example of playing music in the background for your reference

The above is a detailed explanation of the service in Android introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time!

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
分享
二维码
< <上一篇
下一篇>>