Detailed explanation of how Android implements adding custom services in servicemanager

This article describes the method of adding custom services in service manager for Android. Share with you for your reference, as follows:

When we want to use Android system services, we usually use the context. Getsystemservice method. For example, if we want to get AudioManager, we can:

The acquired service is actually a binder service registered in servicemanager, encapsulated and provided to users.

You can see the implementation in contextimpl.java:

AudioManager encapsulates iaudioservice. The actual operations are carried out using iaudioservice. See the code in AudioManager:

The above is how the Android system is used. If we add our own services, what should we do?

We build three test projects in Eclipse:

1) Myservicelib: This is a lib project. You need to check is library in eclipse. For the next two projects, myservicelib needs to be added to the library.

2) Myservice: used to register custom services into servicemanager when Android is powered on. Because the servicemanager is hidden by @ hide, you need to manually add the SDK package to use it. Please refer to the additional instructions for using the @ hide function in the SDK in eclipse. In addition, adding a service requires a system user, so Android: shareduserid = "Android. Uid. System" should be added to the manifest file, and APK should be signed with platform.

3) Myservicetest: used to test the above two projects.

Let's code it.

First create an Aidl file in the myservicelib project. The Android compiler will help us generate the corresponding Java classes. The Aidl file is as follows

Two interfaces are defined for testing, setValue and getValue. The Android compiler will help us generate a Java class of imyservice in the gen directory.

2. Create a myservice class in the myservice project. This class inherits from imyservice.stub and implements setValue and getValue interfaces, which is a service.

Next, we will add it to the service manager.

3. Create myserviceapplication class in myservice project

This is an application. We want to create this application when the Android system starts. In the oncreate method, create the myservice class and add it to the servicemanager. Therefore, I need to modify the manifest file

Note that the application needs the system user and signature to run.

In this way, the server is good, and our service is already in the service manager when it is started.

4. Next, we provide a manager class for the convenience of the client. Create the mymanager class in myservicelib:

5. Test in myservicetest project

Through mymanager. Getinstance(), you can easily get the service manager and call the remote service. We create an activity to use mymanager

Attachment: how to use @ hide function in SDK in eclipse

When we use eclipse for Android development, we use the SDK provided in ADT, which does not contain @ hide functions and variables. Because Android hides these functions when providing SDK for compatibility, security and other reasons. However, many times, we need to use these functions, so we need to add the Android SDK manually. For example, when we use AudioManager, when we need to see whether a certain streamtype is muted, we can call the istreammute (int streamtype) method, but because it is @ hide, we need to introduce our own SDK to compile.

1. When compiling Android system, when compiling "include $(build_java_library)", it will be displayed in $Android_ SOURCE_ BASE/out/target/common/obj/JAVA_ Libraries generates intermediate files. When we need to use some class libraries, we can find them here.

Isstreammute (int streamtype) in framework.jar, we start from out / target / common / obj / Java_ LIBRARIES/framework_ In intermediates, copy classes.jar locally and rename it framework.jar.

2. Right click Project - > properties - > java build path - > Libraries - > add external jar in eclipse

3. Click order and export to put framework.jar on the top

4. Now we can use the istreammute (int streamtype) method in AudioManager

More readers interested in Android related content can view the topics of this site: summary of Android basic component usage, summary of Android view skills, summary of Android resource operation skills, summary of Android operation JSON format data skills, introduction and advanced tutorial of Android development, summary of activity operation skills of Android programming, and Android control usage summary

I hope this article will help you in Android programming.

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