1.创建AIDL文件

path:frameworks/base/core/java/android/app/testmanager/ITestManager.aidlpackage 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.javapackage 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";@Overridepublic String getTestMsg() throws RemoteException {return message;}@Overridepublic void setTestMsg(String msg) throws RemoteException{message=msg;}}
path:frameworks/base/core/java/android/app/testmanager/TestManager.javapackage 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@UnsupportedAppUsagepublic 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.javaprivate 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.javadiff --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:s0telecom                                   u:object_r:telecom_service:s0telephony.registry                        u:object_r:registry_service:s0telephony_ims                             u:object_r:radio_service:s0
+testmanager                               u:object_r:test_manager_service:s0testharness                               u:object_r:testharness_service:s0tethering                                 u:object_r:tethering_service:s0textclassification                        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, systype 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:s0telecom                                   u:object_r:telecom_service:s0telephony.registry                        u:object_r:registry_service:s0telephony_ims                             u:object_r:radio_service:s0
+testmanager                               u:object_r:test_manager_service:s0testharness                               u:object_r:testharness_service:s0tethering                                 u:object_r:tethering_service:s0textclassification                        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, systype 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 failuresto 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文件 就可正常编过。

Android 12 (S) 新加系统服务相关推荐

  1. android+3.0新加的动画,Android动画片

    使用Android两年多了,工作中的动画也动能应付,自认为Android中的动画自己也能用个八九不离十,结果我在学习[Periscope点赞效果](http://www.jianshu.com/p/0 ...

  2. android framework增加新的系统服务

    [android]Framework新增系统服务 分类: android 2014-04-24 17:21  638人阅读  评论(0)  收藏  举报 在android源码中增加一项系统服务,如在a ...

  3. Android 向联系人名单新加联系人(添加名称和电话号码两项)

    //首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactIdContentValues values = new ContentValues( ...

  4. Android 12 “Bug”!除了一加、三星,谷歌自家手机都被“坑”了

    以下内容来自公众号code小生,关注每日干货及时送达 整理 | 郑丽媛 出品 | CSDN(ID:CSDNnews) 相较于前面几个大版本,去年 10 月才问世的 Android 12 正式版稍显迟缓 ...

  5. Android 12 “Bug 连连”:除了一加、三星,谷歌自家手机都被“坑”了

    相较于前面几个大版本,去年 10 月才问世的 Android 12 正式版稍显迟缓:2018 年 8 月 7 日谷歌发布 Android 9 正式版,之后 Android 10 正式版于 2019 年 ...

  6. 更进一步 | Android 12 Beta 2 发布

    作者 / Dave Burke, VP of Engineering 几周前在 Google I/O 上,我们发布了 Android 12 的第一个 Beta 版,带来了能展现您的个性.契合您的需求的 ...

  7. 华为android o适配名单,Android 12首批适配名单公布:没有华为、荣耀

    谷歌已经正式发布了Android 12,新的系统底层带来了3个改动:更丰富的视觉元素,更全面的隐私保护,并且开始引入"多设备互联"的概念.说实话,Android 12的更新只能用& ...

  8. Android 12 发布下载

    本文来自:N软网 nruan.com 原文:https://www.nruan.com/78118.html 在今天的GoogleI/O大会上,Google预览了Android 12,这是今年晚些时候 ...

  9. Android 12 exported自动适配

    最新项目需要升级Android 12,关于Android 12的新特性,大家可以参考Android 12 新特性预览,对于我们开发者来说,必须要适配的两点是:android:exported 和 Sp ...

最新文章

  1. RS-232 Transmitter
  2. delphi 调用php接口_贝壳找房小程序从PHP到Golang的跃迁之路
  3. jQuery中wrap、wrapAll和wrapInner用法以及区别
  4. Ubuntu20.04中安装shutter
  5. GCD介绍(一): 基本概念和Dispatch Queue
  6. 在线apt-get安装mysql_Linux Debain 通过apt-get 方式快速安装Mysql
  7. EasyUI(前端框架)
  8. LeetCode 1090. 受标签影响的最大值(优先队列)
  9. 【英语学习】【WOTD】newspeak 释义/词源/示例
  10. Linux用户基础操作入门
  11. idea 中文字体 自动变_提高工作效率,我推荐讯飞语记,瞬间语音秒变文字
  12. 基于HTML5、JavaScript和CSS3的网页开发应用技术的网页
  13. 小程序性能和体验优化方法
  14. 桌面计算机图标怎么取消,win7图标箭头怎么取消,win7去除电脑桌面图标箭头
  15. 计算机毕业设计SSM高校第二课堂管理系统【附源码数据库】
  16. 谷歌AI论文BERT双向编码器表征模型:机器阅读理解NLP基准11种最优(公号回复“谷歌BERT论文”下载彩标PDF论文)
  17. React 热区组件
  18. html背景边框特效代码,纯JS实现动态边框特效
  19. javascript入门基础篇重点 第二节2.
  20. 用matlab求方程解的三种方法

热门文章

  1. python解决微信登录昵称中带有表情emoji报错,亲测有效。
  2. Python运维开发工程师养成记(条件语句)
  3. waifu2x android,waifu2x
  4. K12在线教育行业痛点及三大破局方法
  5. libtoolize
  6. 多个绝对值相加求最大值问题_多个绝对值相加求最值问题
  7. Java 基础语法 + 常见缩写单词全称(含中文翻译)
  8. 数据分析,一个班级的平均成绩
  9. Servlet的生命周期和线程安全问题
  10. 杨辉三角:给出正整数n(2<=n<=10),输出杨辉三角前n行 杨辉三角性质:三角形中的每个数字等于它两肩上的数字相加