Android 12 (S) 新加系统服务

2023-05-16

1.创建AIDL文件

path:frameworks/base/core/java/android/app/testmanager/ITestManager.aidl


package android.app.testmanager;

interface ITestManager{
        String getTestMsg();
        void setTestMsg(in String msg);

}

2.Context.java添加服务名称

diff --git a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/Context.java
index c3ec094..5a3889c 100644
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -3711,6 +3711,7 @@ public abstract class Context {
             //@hide: SPEECH_RECOGNITION_SERVICE,
             UWB_SERVICE,
             MEDIA_METRICS_SERVICE,
+            TESTMANAGER_SERVICE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ServiceName {}
@@ -4051,6 +4052,19 @@ public abstract class Context {
      */
     public static final String ALARM_SERVICE = "alarm";
 
+
+    
+    /**
+     * Use with {@link #getSystemService(String)} to retrieve a
+     * {@link android.app.testnmanager.TestManager} for receiving intents at a
+     * time of your choosing.
+     *
+     * @see #getSystemService(String)
+     * @see android.app.testnmanager.TestManager
+     */
+    public static final String TESTMANAGER_SERVICE = "testmanager";
+
+
     /**
      * Use with {@link #getSystemService(String)} to retrieve a
      * {@link android.app.NotificationManager} for informing the user of

3.新建TestManager.java和TestManagerService.java

path:frameworks/base/services/core/java/com/android/server/TestManagerService.java


package com.android.server;


import android.app.testmanager.ITestManager;
import android.os.RemoteException;



public class TestManagerService extends ITestManager.Stub {
        static final String TAG = "TestManagerService";
        private String message="test";

        @Override
        public String getTestMsg() throws RemoteException {
                return message;
        }

        @Override
        public void setTestMsg(String msg) throws RemoteException{
                message=msg;
        }



}
path:frameworks/base/core/java/android/app/testmanager/TestManager.java


package android.app.testmanager;

import android.annotation.SystemService;
import android.compat.annotation.UnsupportedAppUsage;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.testmanager.ITestManager;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceManager.ServiceNotFoundException;
import android.util.Singleton;
import android.util.Log;



@SystemService(Context.TESTMANAGER_SERVICE)
public class TestManager {
	private static String TAG = "TestManager";
    private ITestManager mService;
	private static TestManager sInstance;



	/**
	*@hide
	*/
	public  TestManager(ITestManager service){
		mService=service;
		
	}
	
	
	/**
	*@hide
	*/
	@NonNull
	@UnsupportedAppUsage
	public static TestManager getInstance() {
		synchronized (TestManager.class) {
        if (sInstance == null) {

            try {
                IBinder b = ServiceManager.getServiceOrThrow(Context.TESTMANAGER_SERVICE);
                sInstance = new TestManager(ITestManager.Stub
                        .asInterface(ServiceManager.getServiceOrThrow(Context.TESTMANAGER_SERVICE)));
            } catch (ServiceNotFoundException e) {
                throw new IllegalStateException(e);
            }

        }
        return sInstance;
    }
	}

	public String getTestMsg(){
		try{
		   return mService.getTestMsg();
		} catch (RemoteException e){
		   throw e.rethrowFromSystemServer();
		}
	
	}

	 public void setTestMsg(String msg){
                try{
                   mService.setTestMsg(msg);
                } catch (RemoteException e){
                   throw e.rethrowFromSystemServer();
                }
        
        }

	
}     

添加服务到ServiceManager中

path:frameworks/base/services/java/com/android/server/SystemServer.java


private void startOtherServices(){
    ...
    t.traceBegin("StartTestManagerService");
    try {
         ServiceManager.addService(Context.TESTMANAGER_SERVICE,
                 new TestManagerService());
    } catch (Throwable e) {
        Slog.e(TAG, "Failure starting TestManagerService", e);
    }
    t.traceEnd();
    ...

}

将服务添加到ServiceManager中,后续可以根据服务名字获取到这个服务

注册服务

path: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 1202811..6536c70 100644
--- a/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -33,6 +33,8 @@ import android.app.people.PeopleManager;
 import android.app.prediction.AppPredictionManager;
+import android.app.testmanager.TestManager;
 import android.app.role.RoleFrameworkInitializer;
 import android.app.search.SearchUiManager;
 import android.app.slice.SliceManager;
@@ -329,6 +331,16 @@ public final class SystemServiceRegistry {
        ...
+
+       registerService(Context.TESTMANAGER_SERVICE, TestManager.class,
+                new CachedServiceFetcher<TestManager>() {
+            @Override
+            public TestManager createService(ContextImpl ctx) throws ServiceNotFoundException {
+                return TestManager.getInstance();
+            }});
+
         
         registerService(Context.AUDIO_SERVICE, AudioManager.class,
                 new CachedServiceFetcher<AudioManager>() {

Selinux 权限添加

diff --git a/system/sepolicy/prebuilts/api/32.0/private/service_contexts b/system/sepolicy/prebuilts/api/32.0/private/service_contexts
index 7b0f7c9..6fc2770 100644
--- a/system/sepolicy/prebuilts/api/32.0/private/service_contexts
+++ b/system/sepolicy/prebuilts/api/32.0/private/service_contexts
@@ -267,6 +267,7 @@ task                                      u:object_r:task_service:s0
 telecom                                   u:object_r:telecom_service:s0
 telephony.registry                        u:object_r:registry_service:s0
 telephony_ims                             u:object_r:radio_service:s0
+testmanager                               u:object_r:test_manager_service:s0
 testharness                               u:object_r:testharness_service:s0
 tethering                                 u:object_r:tethering_service:s0
 textclassification                        u:object_r:textclassification_service:s0
diff --git a/system/sepolicy/prebuilts/api/32.0/public/service.te b/system/sepolicy/prebuilts/api/32.0/public/service.te
index 3591867..393c70d 100644
--- a/system/sepolicy/prebuilts/api/32.0/public/service.te
+++ b/system/sepolicy/prebuilts/api/32.0/public/service.te
@@ -210,6 +210,7 @@ type textclassification_service, app_api_service, ephemeral_app_api_service, sys
 type textservices_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type texttospeech_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type telecom_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
+type test_manager_service, app_api_service, system_server_service, service_manager_type;
 type thermal_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type timedetector_service, app_api_service, system_server_service, service_manager_type;
 type timezone_service, system_server_service, service_manager_type;
diff --git a/system/sepolicy/private/service_contexts b/system/sepolicy/private/service_contexts
index 7b0f7c9..6fc2770 100755
--- a/system/sepolicy/private/service_contexts
+++ b/system/sepolicy/private/service_contexts
@@ -267,6 +267,7 @@ task                                      u:object_r:task_service:s0
 telecom                                   u:object_r:telecom_service:s0
 telephony.registry                        u:object_r:registry_service:s0
 telephony_ims                             u:object_r:radio_service:s0
+testmanager                               u:object_r:test_manager_service:s0
 testharness                               u:object_r:testharness_service:s0
 tethering                                 u:object_r:tethering_service:s0
 textclassification                        u:object_r:textclassification_service:s0
diff --git a/system/sepolicy/public/service.te b/system/sepolicy/public/service.te
index 3591867..393c70d 100755
--- a/system/sepolicy/public/service.te
+++ b/system/sepolicy/public/service.te
@@ -210,6 +210,7 @@ type textclassification_service, app_api_service, ephemeral_app_api_service, sys
 type textservices_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type texttospeech_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type telecom_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
+type test_manager_service, app_api_service, system_server_service, service_manager_type;
 type thermal_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type timedetector_service, app_api_service, system_server_service, service_manager_type;
 type timezone_service, system_server_service, service_manager_type;

注意需要修改所有的service.te和service_contexts文件,可以参考network_time_update_service 来修改

解决报错

Android 11 以后谷歌强制开启lint检查,lint检查不过编译会报错

  1. frameworks/base/core/java/android/app/testmanager/TestManager.java:58: error: Missing nullability on method getTestMsg return [MissingNullability] lint检查 提示“MissingNullability”

解决方案:加上@Nullable 注解 表明该方法可以返回空

        @Nullable // 加上 @Nullable 表明方法可以返回空
        public String getTestMsg(){
                try{
                   return mService.getTestMsg();
                } catch (RemoteException e){
                   throw e.rethrowFromSystemServer();
                }

        }
        
          // string类型参数 加上@Nullable 表明可以传空参数
        public void setTestMsg(@Nullable String msg){
                try{
                   mService.setTestMsg(msg);
                } catch (RemoteException e){
                   throw e.rethrowFromSystemServer();
                }

      


  1. make update-api报错

out/srcjars/android/app/testmanager/ITestManager.java:165: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:165: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:17: error: Missing nullability on method `asBinder` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:22: error: Raw AIDL interfaces must not be exposed: Stub extends Binder [RawAidl]
out/srcjars/android/app/testmanager/ITestManager.java:30: error: Missing nullability on method `asInterface` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:34: error: Missing nullability on parameter `obj` in method `asInterface` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:45: error: Missing nullability on method `asBinder` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:49: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:49: error: Missing nullability on parameter `data` in method `onTransact` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:49: error: Missing nullability on parameter `reply` in method `onTransact` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:147: error: Missing nullability on parameter `impl` in method `setDefaultImpl` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:160: error: Missing nullability on method `getDefaultImpl` return [MissingNullability]Error: metalava detected the following problems:
out/srcjars/android/app/testmanager/ITestManager.java:165: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:165: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:17: error: Missing nullability on method `asBinder` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:22: error: Raw AIDL interfaces must not be exposed: Stub extends Binder [RawAidl]
8 more error(s) omitted. Search the log for 'error:' to find all of them.
************************************************************
Your API changes are triggering API Lint warnings or errors.
To make these errors go away, fix the code according to the
error and/or warning messages above.

If it is not possible to do so, there are workarounds:

1. You can suppress the errors with @SuppressLint("<id>")
2. You can add a baseline file of existing lint failures
   to the build rule of api-stubs-docs-non-updatable.
************************************************************

解决方案:1.按照提示将报错的地方修改正确 该判空判空 等等
2.让lint检查忽略掉自己的模块 在framework/base 下的Android.bp忽略掉代码检查

metalava_framework_docs_args = "
"--api-lint-ignore-prefix android.mymodule. " //其中 android.mymodule是包名的前缀。

3.以上方法对于添加了AIDL以后 out目录自动生成的文件如: out/srcjars/android/app/testmanager/ITestManager.java 不生效
暂时解决方案:
1.cp out/soong/.intermediates/frameworks/base/api-stubs-docs-non-updatable/android_common/metalava/api-stubs-docs-non-updatable_api.txt frameworks/base/core/api/current.txt
2.将current.txt 中新增的部分对比同步添加到prebuilts/sdk/**/public/api/android.txt
此处** 为prebuilts中SDK最新的版本号,如我代码中最新的是32 所以更新最新的目录即可

diff --git a/prebuilts/sdk/32/public/api/android.txt b/prebuilts/sdk/32/public/api/android.txt
old mode 100644
new mode 100755
index e6f796b..4b4be9b
--- a/prebuilts/sdk/32/public/api/android.txt
+++ b/prebuilts/sdk/32/public/api/android.txt
@@ -8870,6 +8870,37 @@ package android.app.slice {
 
 }
 
+package android.app.testmanager {
+
+  public interface ITestManager extends android.os.IInterface {
+    method public String getTestMsg() throws android.os.RemoteException;
+    method public void setTestMsg(String) throws android.os.RemoteException;
+    field public static final String DESCRIPTOR = "android.app.testmanager.ITestManager";
+  }
+
+  public static class ITestManager.Default implements android.app.testmanager.ITestManager {
+    ctor public ITestManager.Default();
+    method public android.os.IBinder asBinder();
+    method public String getTestMsg() throws android.os.RemoteException;
+    method public void setTestMsg(String) throws android.os.RemoteException;
+  }
+
+  public abstract static class ITestManager.Stub extends android.os.Binder implements android.app.testmanager.ITestManager {
+    ctor public ITestManager.Stub();
+    method public android.os.IBinder asBinder();
+    method public static android.app.testmanager.ITestManager asInterface(android.os.IBinder);
+    method public static android.app.testmanager.ITestManager getDefaultImpl();
+    method public boolean onTransact(int, android.os.Parcel, android.os.Parcel, int) throws android.os.RemoteException;
+    method public static boolean setDefaultImpl(android.app.testmanager.ITestManager);
+  }
+
+  public class TestManager {
+    method @Nullable public String getTestMsg();
+    method public void setTestMsg(@Nullable String);
+  }
+
+}
+
 package android.app.usage {
 
   public final class ConfigurationStats implements android.os.Parcelable {
@@ -11288,6 +11319,7 @@ package android.content {
     field public static final String TELEPHONY_IMS_SERVICE = "telephony_ims";
     field public static final String TELEPHONY_SERVICE = "phone";
     field public static final String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service";
+    field public static final String TESTMANAGER_SERVICE = "testmanager";
     field public static final String TEXT_CLASSIFICATION_SERVICE = "textclassification";
     field public static final String TEXT_SERVICES_MANAGER_SERVICE = "textservices";
     field public static final String TV_INPUT_SERVICE = "tv_input";

总结

经过以上步骤,系统服务就能正常起来了,添加过程中主要问题就是 强制lint检查,导致AIDL自动生成的文件报错,目前通过报错打印:–api-lint ./out/.intermediates/prebuilts/sdk/android.api.public.latest/gen/android.api.public.latest 初步发现是api
会和android.api.public.latest 比对,如果不一致就会报错,可能是谷歌不允许 随便乱添加东西到framework中。 这个文件 发现是根据prebuilts/sdk/**/public/api/android.txt生成的,名字根据路径来取的,所以我目前就直接修改了这个prebuilts 里面的文件 这样比对的样本就和自己添加的一致,lint检查也就能顺利通过
非正规手段,如有更好的办法欢迎留言。
如果想添加 setting.Global key 也是相同 更新一下prebuilts/sdk/**/public/api/android.txt文件 就可正常编过。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 12 (S) 新加系统服务 的相关文章

  • 从 Throwable 获取错误代码 - Android

    我怎样才能从错误代码可投掷 https developer android com reference java lang Throwable html public void onFailure Throwable exception 我
  • Android SoundPool 堆限制

    我正在使用 SoundPool 加载多个声音剪辑并播放它们 据我所知 它的功能 100 正确 但在 load 调用期间 我的日志中充斥着以下内容 06 09 11 30 26 110 ERROR AudioCache 23363 Heap
  • 如何将安卓手机从睡眠状态唤醒?

    如何以编程方式将 Android 手机从睡眠状态唤醒 挂起至内存 我不想获取任何唤醒锁 这意味着手机在禁用 CPU 的情况下进入 真正的 睡眠状态 我想我可以使用某种RTC 实时时钟 机制 有人有例子吗 Thanks 为了让Activity
  • Android libgdx 首选项丢失

    我在 Libgdx 引擎中创建了 Android 游戏 一段时间后 我注意到在某些应用程序杀手中杀死该应用程序后 或者如果我在 Android 设置中执行 强制关闭 操作 我保存到首选项中的游戏选项就会丢失 有办法防止这种情况吗 我从来没有
  • KitKat(及更低版本)设备上的 Android Material Design

    我将在我们学校开发一个 Android 应用程序作为一个项目 我想使用 Google 的新 Material Design 但我知道它仅适用于 Android L 设备 Jack Underwood 最近发布了名为 Today Calend
  • RxJava、Proguard 和 sun.misc.Unsafe

    我有以下问题RxJava 1 1 0 使用时Proguard 我没有更改 RxJava 版本或其 pro文件 但更新后OkHttp我无法编译使用Proguard因为我有关于sun misc Unsafe不在场 rxJava pro keep
  • 接近语法错误(代码1)插入Android SQLite

    我正在创建一个通讯录应用程序 用户可以在其中输入姓名 电子邮件地址和号码 我希望将此数据保存在数据库中 但我似乎无法使插入方法起作用 我收到的错误是 android database sqlite SQLiteException near
  • 设置从 Facebook 登录获取用户电子邮件 ID 的权限

    我在用着Facebook 3 0 SDK对于安卓 我必须实施Facebook登录 我正在访问用户的基本信息 例如姓名 用户 ID 但我也想访问用户的电子邮件 我浏览了很多博客和论坛 但不知道该怎么做 我正在使用我自己的 android 按钮
  • 如果我们使用后退按钮退出,为什么 Android 应用程序会重新启动?

    按住主页按钮并返回应用程序时 应用程序不会重新启动 为什么使用后退按钮会重新启动 如果我们使用后退按钮退出 有什么方法可以解决在不重新启动的情况下获取应用程序的问题吗 请帮忙 当您按下Home按钮 应用程序将暂停并保存当前状态 最后应用程序
  • 获取 AlarmManager 中活动的 PendingIntents 列表

    我有办法获取活动列表PendingIntent在设备中 我开始工作AlarmManager我想看看我的PendingIntents 已正确创建和删除 也很高兴看到其他什么PendingIntent在那里 只是为了看看某些应用程序是否正在做一
  • TextView 之间有分隔线

    我正在尝试在 android studio 中创建以下布局 因为我对 android 东西还很陌生 所以我第一次尝试使用 LinearLayout 并认为这可能无法实现 现在我正在尝试使用RelativeLayout 我已经用颜色创建了这个
  • Android 版 Robotium - solo.searchText () 不起作用

    我在使用 Robotium 时遇到 searchText 函数问题 我正在寻找这个字符串
  • Flutter 深度链接

    据Flutter官方介绍深层链接页面 https flutter dev docs development ui navigation deep linking 我们不需要任何插件或本机 Android iOS 代码来处理深层链接 但它并没
  • MediaCodec 创建输入表面

    我想使用 MediaCodec 将 Surface 编码为 H 264 使用 API 18 有一种方法可以通过调用 createInputSurface 然后在该表面上绘图来对表面中的内容进行编码 我在 createInputSurface
  • Android Studio:无法启动守护进程

    当我尝试在 Android Studio 中导入 gradle 项目时 遇到以下错误 Unable to start the daemon process This problem might be caused by incorrect
  • 如何将设备连接到Eclipse?

    我无法解决这个简单的问题 我正在尝试通过 USB 电缆将我的设备连接到 Eclipse 在我的 PC 上 我已经安装了 Eclipse 和 Android SDK 并且在模拟器上运行该程序运行良好 我已在我的电脑上下载并安装了 Samsun
  • 下载后从谷歌照片库检索图像

    我正在发起从图库中获取照片的意图 当我在图库中使用 Nexus 谷歌照片应用程序时 一切正常 但如果图像不在手机上 在 Google Photos 在线服务上 它会为我下载 选择图像后 我将图像发送到另一个活动进行裁剪 但在下载的情况下 发
  • 保护 APK 中的字符串

    我正在使用 Xamarin 的 Mono for Android 开发一个 Android 应用程序 我目前正在努力使用 Google Play API 添加应用内购买功能 为此 我需要从我的应用程序内向 Google 发送公共许可证密钥
  • Android:有没有办法以毫安为单位获取设备的电池容量?

    我想获取设备的电池容量来进行一些电池消耗计算 是否可以以某种方式获取它 例如 三星 Galaxy Note 2 的电池容量为 3100mAh 谢谢你的帮助 知道了 在 SDK 中无法直接找到任何内容 但可以使用反射来完成 这是工作代码 pu
  • 找到 Android 浏览器中使用的 webkit 版本?

    有没有办法知道某些特定手机上的 Android 浏览器使用的是哪个版本的 webkit 软件 如果有一个您可以浏览以获取该信息的 URL 那就太好了 但任何其他方式也很好 如果你知道 webkit 版本 你就知道 html5 支持多少 至少

随机推荐