基于AOSP 13版本。
实施步骤:
AOSP/demo/services/src/com/chen/services
package com.chen.services;
import android.content.Context;
import android.os.Binder;
import android.os.Parcel;
import android.os.RemoteException;
import com.android.server.SystemService;
import java.util.concurrent.CountDownLatch;
// 这里的继承Binder,可以改成自己的AIDL.Stub类
public class DemoSystemService extends Binder {
private Context mContext;
public DemoSystemService(Context context){
mContext = context;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void systemReady() {
// initSomething();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void initSomething(){
}
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
public static class LifeCycle extends SystemService {
private DemoSystemService mService;
private final CountDownLatch mCountDown = new CountDownLatch(1);
/**
* Initializes the system service.
*
* Subclasses must define a single argument constructor that accepts the context
* and passes it to super.
*
*
* @param context The system server context.
*/
public LifeCycle(Context context) {
super(context);
mService = new DemoSystemService(context);
}
@Override
public void onStart() {
// 父类的方法
publishBinderService("demo",mService);
}
@Override
public void onBootPhase(int phase) {
switch (phase) {
case SystemService.PHASE_ACTIVITY_MANAGER_READY:
mService.systemReady();
break;
default:
break;
}
}
}
}
demo/services/src/Android.bp
filegroup {
name: "demo--system-service-sources",
srcs: ["com/chen/services/DemoSystemService.java"],
}
修改frameworks/base/services/Android.bp, diff如下:
diff --git a/services/Android.bp b/services/Android.bp
index ccdf7ca06924..d26ffd533292 100644
--- a/services/Android.bp
+++ b/services/Android.bp
@@ -132,7 +132,9 @@ java_library {
profile: "art-profile",
},
- srcs: [":services-main-sources"],
+ srcs: [":services-main-sources",
+ ":demo--system-service-sources"
+ ],
// The convention is to name each service module 'services.$(module_name)'
static_libs: [
修改frameworks/base/services/java/com/android/server/SystemServer.java, 启动DemoSystemService,diff如下:
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 46141940a907..5d99ebe7842b 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -213,6 +213,7 @@ import com.android.server.webkit.WebViewUpdateService;
import com.android.server.wm.ActivityTaskManagerService;
import com.android.server.wm.WindowManagerGlobalLock;
import com.android.server.wm.WindowManagerService;
+import com.chen.services.DemoSystemService;
import dalvik.system.VMRuntime;
@@ -2141,6 +2142,10 @@ public final class SystemServer implements Dumpable {
}
t.traceEnd();
+ t.traceBegin("StartDemoSystemService");
+ mSystemServiceManager.startService(DemoSystemService.LifeCycle.class);
+ t.traceEnd();
+
t.traceBegin("StartSoundTriggerMiddlewareService");
mSystemServiceManager.startService(SoundTriggerMiddlewareService.Lifecycle.class);
t.traceEnd();
修改system/sepolicy库,添加demo服务的标签和规则,注意添加的服务名称为demo,(publishBinderService(“demo”,mService)
diff如下:
diff --git a/prebuilts/api/33.0/private/compat/32.0/32.0.ignore.cil b/prebuilts/api/33.0/private/compat/32.0/32.0.ignore.cil
index 076d642e2..f7733c4d9 100644
--- a/prebuilts/api/33.0/private/compat/32.0/32.0.ignore.cil
+++ b/prebuilts/api/33.0/private/compat/32.0/32.0.ignore.cil
@@ -82,4 +82,5 @@
vendor_vm_file
virtual_device_service
wallpaper_effects_generation_service
+ demo_service
))
diff --git a/prebuilts/api/33.0/private/service_contexts b/prebuilts/api/33.0/private/service_contexts
index 72fa16629..6cdb000eb 100644
--- a/prebuilts/api/33.0/private/service_contexts
+++ b/prebuilts/api/33.0/private/service_contexts
@@ -324,6 +324,7 @@ statscompanion u:object_r:statscompanion_service:s0
statsmanager u:object_r:statsmanager_service:s0
soundtrigger u:object_r:voiceinteraction_service:s0
soundtrigger_middleware u:object_r:soundtrigger_middleware_service:s0
+demo u:object_r:demo_service:s0
statusbar u:object_r:statusbar_service:s0
storaged u:object_r:storaged_service:s0
storaged_pri u:object_r:storaged_service:s0
diff --git a/prebuilts/api/33.0/public/service.te b/prebuilts/api/33.0/public/service.te
index e862b405f..fdc84c1a0 100644
--- a/prebuilts/api/33.0/public/service.te
+++ b/prebuilts/api/33.0/public/service.te
@@ -218,6 +218,7 @@ type system_config_service, system_api_service, system_server_service, service_m
type system_server_dumper_service, system_api_service, system_server_service, service_manager_type;
type system_update_service, system_server_service, service_manager_type;
type soundtrigger_middleware_service, system_server_service, service_manager_type;
+type demo_service, system_server_service, service_manager_type;
type speech_recognition_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
type tare_service, app_api_service, system_server_service, service_manager_type;
type task_service, system_server_service, service_manager_type;
diff --git a/private/compat/32.0/32.0.ignore.cil b/private/compat/32.0/32.0.ignore.cil
index 076d642e2..f7733c4d9 100644
--- a/private/compat/32.0/32.0.ignore.cil
+++ b/private/compat/32.0/32.0.ignore.cil
@@ -82,4 +82,5 @@
vendor_vm_file
virtual_device_service
wallpaper_effects_generation_service
+ demo_service
))
diff --git a/private/service_contexts b/private/service_contexts
index 72fa16629..6cdb000eb 100644
--- a/private/service_contexts
+++ b/private/service_contexts
@@ -324,6 +324,7 @@ statscompanion u:object_r:statscompanion_service:s0
statsmanager u:object_r:statsmanager_service:s0
soundtrigger u:object_r:voiceinteraction_service:s0
soundtrigger_middleware u:object_r:soundtrigger_middleware_service:s0
+demo u:object_r:demo_service:s0
statusbar u:object_r:statusbar_service:s0
storaged u:object_r:storaged_service:s0
storaged_pri u:object_r:storaged_service:s0
diff --git a/public/service.te b/public/service.te
index e862b405f..fdc84c1a0 100644
--- a/public/service.te
+++ b/public/service.te
@@ -218,6 +218,7 @@ type system_config_service, system_api_service, system_server_service, service_m
type system_server_dumper_service, system_api_service, system_server_service, service_manager_type;
type system_update_service, system_server_service, service_manager_type;
type soundtrigger_middleware_service, system_server_service, service_manager_type;
+type demo_service, system_server_service, service_manager_type;
type speech_recognition_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
type tare_service, app_api_service, system_server_service, service_manager_type;
type task_service, system_server_service, service_manager_type;
经过上面的修改,编译运行时,system_server进程就可以找到DemoSystemService类,并且在启动服务时赋予此服务对应的SE身份。
在模拟器里,可以查看到新添加的demo服务:
emulator_car_x86_64:/ $ dumpsys -l |grep demo
demo
1,demo库可以改为java_library_static形式,可参考:
frameworks/base/services/searchui/Android.bp
filegroup {
name: "services.searchui-sources",
srcs: ["java/**/*.java"],
path: "java",
visibility: ["//frameworks/base/services"],
}
java_library_static {
name: "services.searchui",
defaults: ["platform_service_defaults"],
srcs: [":services.searchui-sources"],
libs: ["services.core"],
}
2,services依赖此模块,可以参考如下,通过static_libs方式引用:
/home/chen/disk1/work/projects/aosp/android-13.0.0_r41/frameworks/base/services/Android.bp
java_library {
name: "services",
defaults: ["services_java_defaults"],
installable: true,
dex_preopt: {
app_image: true,
profile: "art-profile",
},
srcs: [":services-main-sources",
":demo--system-service-sources"
],
// The convention is to name each service module 'services.$(module_name)'
static_libs: [
"services.core",
"services.accessibility",
"services.appprediction",
"services.appwidget",
"services.autofill",
"services.backup",
"services.companion",
"services.contentcapture",
"services.contentsuggestions",
"services.coverage",
"services.devicepolicy",
"services.midi",
"services.musicsearch",
"services.net",
"services.people",
"services.print",
"services.profcollect",
"services.restrictions",
"services.searchui",//参考此处
......