haohao

在 Android 系统源码中自定义系统服务

Markdown

一件事如果犹豫做还是不做,选“做”。 做了也可能会后悔,但是这种后悔和“没做”而产生的一辈子的妄念相比,坏处小多了。

本篇详细介绍一下在 Android 系统源码中自定义系统服务的过程。

在 Android 系统源码中自定义系统服务(Custom System Service in AOSP)

配置编译环境(Initial AOSP build environment.)

cd AOSP root dir
source build/envsetup.sh
lunch
2

定义 Service 的 AIDL 文件(Define service AIDL file)

path: frameworks/base/core/java/android/os/
Create IHaoHaoService.aidl file

1
2
3
4
5
6
7
8
package android.os;
interface IHaoHaoService {
void setVal(String key,String value);
String getVal(String key);
}

修改 Android 模块文件(Modify Android.mk file)

path: frameworks/base/Android.mk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
......
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += \
......
core/java/android/os/IPowerManager.aidl \
core/java/android/os/IRemoteCallback.aidl \
core/java/android/os/ISchedulingPolicyService.aidl \
core/java/android/os/IUpdateLock.aidl \
core/java/android/os/IUserManager.aidl \
core/java/android/os/IVibratorService.aidl \
core/java/android/os/IHaoHaoService.aidl \
......

进行模块编译:

~/aosp/android-6.0.1_r1$ mmm frameworks/base

generate IHaoHaoService.java file.

创建 HaoHaoService 文件(Create HaoHaoService file)

path: frameworks/base/services/core/java/com/android/server/
Create HaoHaoService.java file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.android.server;
import android.util.Slog;
import android.os.RemoteException;
import android.os.IHaoHaoService;
import java.util.HashMap;
public class HaoHaoService extends IHaoHaoService.Stub {
private static HashMap<String,String> mCache = null;
private static final String TAG="HaoHaoService";
public HaoHaoService() {
mCache = new HashMap<>();
Slog.d(TAG, "HaoHaoService starting.");
}
public void setVal(String key, String value) throws RemoteException {
mCache.put(key, value);
}
public String getVal(String key) throws RemoteException {
return mCache.get(key);
}
}

注册服务(Register service)

Modify frameworks/base/core/java/android/content/Context.java
Add:

1
2
3
4
5
6
7
8
/**
* Use with {@link #getSystemService} to retrieve a
* {@link android.os.IHaoHaoService} for accessing the HaoHao service.
*
* @see #getSystemService
* @hide
*/
public static final String HAOHAO_SERVICE = "haohao";

Modify frameworks/base/services/java/com/android/server/SystemServer.java
Find function startOtherServices()
Add:

1
2
3
4
5
6
7
try {
Slog.i(TAG, "***HaoHao Service***");
ServiceManager.addService(Context.HAOHAO_SERVICE, new HaoHaoService());
} catch (Throwable e) {
Slog.e(TAG, "Failure starting HaoHao Service", e);
}

Create HaoHaoServiceManager.java file

path: frameworks/base/core/java/android/app/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package android.app;
/**
* Created by haohao on 17/10/2.
*/
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.IHaoHaoService;
import android.util.Log;
public class HaoHaoServiceManager {
private static final String TAG = "HaoHaoServiceManager";
private IHaoHaoService mService;
public HaoHaoServiceManager(Context context, IHaoHaoService service){
mService = service;
}
public void setVal(String key,String value){
try {
mService.setVal(key,value);
} catch(Exception e){
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
public String getVal(String key){
try {
return mService.getVal(key);
} catch(Exception e){
Log.e(TAG, e.toString());
e.printStackTrace();
}
return null;
}
}

Register service in SystemServiceRegistry.java file

path: frameworks/base/core/java/android/app/SystemServiceRegistry.java
Add:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import android.os.IHaoHaoService;
......
registerService(Context.HAOHAO_SERVICE, HaoHaoServiceManager.class,
new CachedServiceFetcher<HaoHaoServiceManager>() {
@Override
public HaoHaoServiceManager createService(ContextImpl ctx) {
IBinder binder = ServiceManager.getService(Context.HAOHAO_SERVICE);
IHaoHaoService service = IHaoHaoService.Stub.asInterface(binder);
return new HaoHaoServiceManager(ctx, service);
}});
......

Modify SePolicy Build Check

path: external/sepolicy/service.te

Add:

1
type haohao_service, system_api_service, system_server_service, service_manager_type;

path: external/sepolicy/service_contexts

Add:

1
haohao u:object_r:haohao_service:s0

service_contexts

1
2
3
4
5
6
7
accessibility u:object_r:accessibility_service:s0
account u:object_r:account_service:s0
activity u:object_r:activity_service:s0
alarm u:object_r:alarm_service:s0
haohao u:object_r:haohao_service:s0
android.security.keystore u:object_r:keystore_service:s0
android.service.gatekeeper.IGateKeeperService u:object_r:gatekeeper_service:s0

Update api and build

~/aosp/android-6.0.1_r1$ make update-api -j8

~/aosp/android-6.0.1_r1$ make -j8

Test service in your Activity

1
2
3
4
5
6
@Override
public void onCreate() {
super.onCreate();
mHaoHaoServiceManager = (HaoHaoServiceManager)getSystemService(Context.HAOHAO_SERVICE);
mHaoHaoServiceManager.setVal("haohao", "Android Developer");


联系我

Wechat ID

公众号

生活不止于眼前的苟且, 还有诗和远方的田野