First, Android system services
Android offers a lot of system services: such as Activitymanger,powermanger,windowmanger,wifimanger and so on.
These services are always available at the beginning of the system startup, and if the app needs to use the appropriate system services (such as getting the status of the current WiFi), it needs to access the system services via binder.
For example: Wifimanager Mwifimanager = (wifimanager) context.getsystemservice (Context.wifi_service);
Ii. Binder Inter-process communication
How the app communicates between processes via binder (usually one app to use another app), see Android Bindservice () and Android Aidl
By looking at the source code we can see that, in general, the system service defines an open interface through Aidl. The overall call relationship is the same as the app interprocess communication.
Third, set the service as system service
The service provided by the General app must be started manually or powered on, and app-level service is more likely to be killed when resources are scarce.
So can we set the service that needs to be resident to be system service?
Steps:
1. We follow the original system services (such as Activitymanger,powermanger,windowmanger,wifimanger) and register the relevant services.
For example, we want the newly registered system service to be called Demosystemservice.
We found where the native system service was registered, Systemserver.java's Startotherservices () method, with the following code, to complete the registration of the Demosystemservice
1 Try {2 SLOG.I (TAG, "Demosystem service"); 3 Servicemanager.addservice ("Demosystemservice",4 new Demosystemmanager ( context)); 5 Catch (Throwable e) {6 reportwtf ("Starting Demosystemservice", e); 7 }
2. According to AIDL design requirements, Demosystemservice must provide the corresponding Aidl file, and implement the standard interface of AIDL
First aidl the standard interface setting
Package Android.app; Interface Idemosystemmanager { boolean getState (); void SetState (boolean state );}
Then the content of the specific system services, in line with the AIDL standard interface
PackageCom.android.server; ImportAndroid.app.IDemoSystemManager; ImportAndroid.util.Log; ImportAndroid.content.Context; Public classDemosystemmanagerextendsIdemosystemmanager.stub {Private Static FinalString TAG = "Demosystemmanager"; PrivateContext Mcontext; Private Booleanstate = False; PublicDemosystemmanager (Context context) {Mcontext=context; }
Public BooleangetState () {log.d (TAG,"[GetState] State:" +State ); returnState ; } Public voidSetState (BooleanState ) {LOG.D (TAG,"[SetState] State:" +State ); return0; }} 3. To this end, the service has been written. Here's how the client uses the service.
Import Android.hardware.DemoSystemManger; Private Idemosystemmanager Mdemosystemmanager; = IDemoSystemManager.Stub.asInterface (Servicemanager.getservice ("Demosystemservice"));
And then you can use Mdemosystemmanger directly.
4. Other content to configure a. Place the Aidl file in the compilation android.mk
In frameworks/base/-6 +, 7 @@ local_src_files + + Core /java/android/app/backup/ifullbackuprestoreobserver.aidl core/java/android/app/backup/ Irestoreobserver.aidl core/java/android/app/backup/+ core/java/android/app/ idemosystemmanger.aidl \ core/java/android/bluetooth/ibluetooth.aidl Core/java/android/bluetooth/ibluetootha2dp.aidl \
After b.android5.0, if you want to create a new system service, you also need to add permissions (this is the impact of SELinux, SELinux related content can be online to check other articles)
In the/device/sprd/scx35/sepolicy/service_contexts file, add the permissions Atchannel U:OBJECT_R:RADIO_SERVICE:S0FM u:o Bject_r:mediaserver_service:s0sprdsimphonebook u:object_r:radio_service:s0sprd_phone u:object_r:radio_ Service:s0security u:object_r:system_server_service:s0board_score u:object_r:system_server_service: S0phasechecknative u:object_r:system_server_service:s0demosystemservice u:object_r:system_server_ Service:s0
Note: The example in this article is a very simple system service, if you want to look at the example of a relatively complex point, you can look at the following examples in the article, this example is more vivid, the code structure is more reasonable.
Reference article:
[Android] Framework new System Services
Android system services at a glance
New Android system service