1.新建aidl接口文件,其中/frameworks/base/core/java/com 目录下的custom文件夹是新增的
/frameworks/base/core/java/com/custom/ICustomService.aidl
package com.custom;
/**
* @hide
*/
interface ICustomService {
}
2.创建服务Manager类
/frameworks/base/core/java/com/custom/CustomManager.java
package com.custom;
import com.custom.ICustomService;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;
import android.annotation.NonNull;
/**
* CustomManager 类是客户端访问 CustomService 的入口类。
*/
public class CustomManager {
private static final String TAG = "CustomManager";
/**
* 服务接口实例。
*/
private ICustomService mService;
/**
* @param context 应用程序上下文。
* @param service
* @hide
*/
public CustomManager(@NonNull Context context, @NonNull ICustomService service) {
mService = service;
}
}
3.创建服务端Service
/frameworks/base/services/core/java/com/custom/CustomService.java
package com.custom;
import com.custom.ICustomService;
import android.util.Log;
import android.content.Context;
/**
* 该类继承自ICustomService.Stub
*/
public class CustomService extends ICustomService.Stub {
private static final String TAG = "CustomService";
/**
* 上下文
*/
private Context mContext;
public CustomService(Context context) {
mContext = context;
android.util.Log.d(TAG, "CustomService run ...");
}
}
4.创建系统服务名称
/frameworks/base/core/java/android/content/Context.java
diff --git a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/Context.java
index 0f13ffa393..a0c9c9c263 100644
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -3494,7 +3494,8 @@ public abstract class Context {
//@hide: TIME_ZONE_DETECTOR_SERVICE,
PERMISSION_SERVICE,
LIGHTS_SERVICE,
+ CUSTOM_SERVICE
})
@Retention(RetentionPolicy.SOURCE)
public @interface ServiceName {}
@@ -3950,6 +3951,12 @@ public abstract class Context {
+ /**
+ *CUSTOM_SERVICE
+ */
+ @SuppressLint("ServiceName")
+ public static final String CUSTOM_SERVICE = "custom_service";
+
/**
* Use with {@link #getSystemService(String)} to retrieve a {@link
* android.app.StatusBarManager} for interacting with the status bar.
5.注册系统服务
/frameworks/base/core/java/android/app/SystemServiceRegistry.java
diff --git a/frameworks/base/core/java/android/app/SystemServiceRegistry.java b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
index e55fed8094..62e41f1d6e 100644
--- a/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -18,6 +18,8 @@ package android.app;
+import com.custom.ICustomService;
+import com.custom.CustomManager;
import android.accounts.AccountManager;
import android.accounts.IAccountManager;
import android.annotation.NonNull;
@@ -798,6 +800,15 @@ public final class SystemServiceRegistry {
+ registerService(Context.CUSTOM_SERVICE, CustomManager.class,
+ new CachedServiceFetcher<CustomManager>() {
+ @Override
+ public CustomManager createService(ContextImpl ctx) {
+ IBinder b = ServiceManager.getService(Context.CUSTOM_SERVICE);
+ ICustomService service = ICustomService.Stub.asInterface(b);
+ return new CustomManager(ctx, service);
+ }});
+
registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
new CachedServiceFetcher<AppOpsManager>() {
@Override
6.添加到系统服务中
/frameworks/base/services/java/com/android/server/SystemServer.java
diff --git a/frameworks/base/services/java/com/android/server/SystemServer.java b/frameworks/base/services/java/com/android/server/SystemServer.java
index aea271d7bf..4a863cd1f2 100755
--- a/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -17,6 +17,7 @@
package com.android.server;
+import com.custom.CustomService;
import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_CRITICAL;
import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_HIGH;
@@ -1071,6 +1072,13 @@ public final class SystemServer {
+ try {
+ ServiceManager.addService(Context.CUSTOM_SERVICE, new CustomService(context));
+ Slog.i("custom", "Service Registration Completed");
+ } catch (Throwable e) {
+ Slog.e("custom", "Service Registration Failed", e);
+ }
+
try {
final String SECONDARY_ZYGOTE_PRELOAD = "SecondaryZygotePreload";
// We start the preload ~1s before the webview factory preparation, to
7.加入到package_allowed_list.txt
/build/make/core/tasks/check_boot_jars/package_allowed_list.txt
diff --git a/build/make/core/tasks/check_boot_jars/package_allowed_list.txt b/build/make/core/tasks/check_boot_jars/package_allowed_list.txt
index ebfd850a37..f0c210fbc3 100644
--- a/build/make/core/tasks/check_boot_jars/package_allowed_list.txt
+++ b/build/make/core/tasks/check_boot_jars/package_allowed_list.txt
@@ -250,3 +250,6 @@ org\.chromium\.arc\..*
+com\.custom
+com\.custom\..*
8.更新API,因为上面增加了系统服务所有需要执行以下命令更新API
make update-api
9.编译Android
./build.sh -Au
10.烧录update.img