This article provides a simple example of how to add a system service to the Android 6.0 system and how to use custom system services.
Goal: 1. Create a custom service Cccservice
The 2.APP development process can use Getsystemservice ("CCC") to get Cccmanager and invoke functions inside.
Step1 Creating a Aidl file
Add a icccservice.aidl below the source frameworks/base/core/java/android/os/
Suppose we define 5 functions that will be executed in the systemserver process.
The contents are as follows
//Icccservice.aidl PackageAndroid.os;//Declare any non-default types here with import statementsInterfaceIcccservice {/*** Demonstrates some basic types that's can use as parameters * and return values in Aidl. */ voidsetval (String key,string value); String Getval (string key); voidAppendlog (String log); voidClearlog (); String Readlog ();}
Step2 Creating a service file (Cccservice)
A new interface Cccservice.java is used to implement the Aidl file definition is added under frameworks/base/services/core/java/com/android/server/.
The contents are as follows
PackageCom.android.server;ImportJava.io.InputStreamReader;ImportJava.io.LineNumberReader;Importjava.lang.*;ImportJava.util.HashMap;Importandroid.os.RemoteException;ImportAndroid.os.ICCCService;/*** * Created by Zhuangqianliu on 2016/9/21.*/ Public classCccserviceextendsIcccservice.stub {Private StaticHashmap<string,string> map=NewHashmap<>(); Private StaticString inner_log= ""; PublicCccservice () {} @Override Public voidSetval (string key, String value)throwsremoteexception {map.put (key,value); } @Override PublicString Getval (String key)throwsRemoteException {returnMap.get (key); } @Override Public voidAppendlog (String log)throwsremoteexception {inner_log+=log+ "\ n"; } @Override Public voidClearlog ()throwsremoteexception {inner_log=""; } @Override PublicString Readlog ()throwsRemoteException {returnInner_log; }}
Step3 Adding a custom service to the Systemserver startup process
Add a row to the Frameworks/base/core/java/android/content/context.java first
public static final String ccc_service= "CCC";
Modify Frameworks/base/services/java/com/android/server/systemserver.java
Add the following code in the try module of the startotherservices () function
Try { "CCC Service"); New Cccservice ()); Catch (Throwable e) { "Failure starting CCC Service", E); }
Final effect
STEP4 create manager, i.e. Cccmanager
Under frameworks/base/core/java/android/app/, create the Cccmanager.java file content as follows
PackageAndroid.app;/*** Created by Liam on 16/10/2.*/Importandroid.annotation.SdkConstant;ImportAndroid.annotation.SystemApi;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Build;ImportAndroid.os.Parcel;Importandroid.os.Parcelable;Importandroid.os.RemoteException;ImportAndroid.os.ICCCService;ImportAndroid.util.Log; Public classCccmanager {icccservice mservice; PublicCccmanager (Context ctx,icccservice service) {Mservice=Service; } Public voidsetval (String key,string value) {Try{mservice.setval (key,value); }Catch(Exception e) {LOG.E ("Cccmanager", e.tostring ()); E.printstacktrace (); } } Publicstring Getval (String key) {Try{ returnMservice.getval (key); }Catch(Exception e) {LOG.E ("Cccmanager", e.tostring ()); E.printstacktrace (); } return NULL; } Public voidAppendlog (String log) {Try{mservice.appendlog (log); }Catch(Exception e) {LOG.E ("Cccmanager", e.tostring ()); E.printstacktrace (); } } Public voidClearlog () {Try{mservice.clearlog (); }Catch(Exception e) {LOG.E ("Cccmanager", e.tostring ()); E.printstacktrace (); } } PublicString Readlog () {Try{ returnMservice.readlog (); }Catch(Exception e) {LOG.E ("Cccmanager", e.tostring ()); E.printstacktrace (); } return NULL; }}
STEP5 Register to Systemservice
Modify Frameworks/base/core/java/android/app/systemserviceregistry.java
Add in static code block
Registerservice (Context.ccc_service, Cccmanager. Class, new cachedservicefetcher<cccmanager>() { @Override public Cccmanager CreateService (Contextimpl ctx) { = Servicemanager.getservice (context.ccc_ SERVICE); = ICCCService.Stub.asInterface (b); return New Cccmanager (CTX, service); });
STEP6 modifying compilation validation for Sepolicy
Modify/external/sepolicy/service.te
Add on last line
Type Ccc_service, System_api_service, System_server_service, Service_manager_type;
Then modify the/external/sepolicy/service_contexts file in the same directory
Insert a row in the middle
CCC U:object_r:ccc_service:s0
STEP7 re-compiling the source code
Don't forget to make Update-api first.
STEP8 Test
Tip
You can create a Java project to write a simple Cccmanager class and export the jar to use provided dependencies in the IDE. This will not be an error in the development process.
Java Temporary project is the following Java project created using idea
Android Studio project configuration
Android Source Add custom system service