RK3566/RK3568 Android 11添加系统服务

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

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值