当前位置:   article > 正文

Android framework层添加三方应用接口,及添加系统Service(转)_android11 framework添加自定义接口

android11 framework添加自定义接口

亲测可用Android原生代码添加系统服务

2017年01月12日 21:55:56

阅读数:5492

在Android系统中,为我们提供了很多的系统服务,比如AMS,PMS等,今天参考系统里其他服务的实现,在Android N原生代码中添加自己定制的服务,想想都满激动的

准备工作

在正式开始之前,需要知道下面两点以及满足下面条件:

前提条件

Android原生代码,可以通过make全编通过,编译完成之后,可以通过emulator命令启动out目录下生成的image文件,需要注意在执行emulator命令之前,需要执行source build/envsetup.sh构建环境,以及lunch选择产品

如何调用系统服务

  1. // 比如WindowManagerService的使用
  2. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  • 系统服务的注册流程

其实跟着上面的context.getSystemService代码,就可以知道系统服务的注册流程,实际上ContextImpl也是通过SystemServiceRegistry.getSystemService来获取具体的服务,这些系统服务都是在SystemServiceRegistry的static静态代码块中进行注册的

  1. static {
  2. .....
  3. registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
  4. new CachedServiceFetcher<AccountManager>() {
  5. @Override
  6. public AccountManager createService(ContextImpl ctx) {
  7. IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE);
  8. IAccountManager service = IAccountManager.Stub.asInterface(b);
  9. return new AccountManager(ctx, service);
  10. }});
  11. registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
  12. new CachedServiceFetcher<ActivityManager>() {
  13. @Override
  14. public ActivityManager createService(ContextImpl ctx) {
  15. return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
  16. }});
  17. .....
  18. }

具体可以参考Android中getSystemService流程

下面就是照葫芦画瓢的步骤

定义ISelfManager.aidl文件

系统里面很多的aidl文件定义在/frameworks/base/core/java/android/os下,所以我们需要做的就是参考其他的aidl,照样子写一个简单的ISelfManager.aidl

/frameworks/basecore/java/android/os/ISelfManager.aidl

  1. package android.os;
  2. interface ISelfManager {
  3. int selfAddNumber(int numberFirst, int numberSecond);
  4. String selfAddString(String originalStr);
  5. }
  • 更新Android.mk文件

在frameworks/base/Android.mk文件的LOCAL_SRC_FILES中系统添加了很多aidl文件,我们需要添加自己定义的ISelfManager.aidl文件

  1. LOCAL_SRC_FILES += \
  2. core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \
  3. core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl \
  4. .....
  5. core/java/android/os/ISelfManager.aidl \

然后使用mmm frameworks/base,此时会自动根据aidl文件生成对应的stub接口,其实这里没有生成也没有关系,我们可以自己将该文件添加到Android studio中,让ADT自动生成也可以,这里主要是方便后面编写实现类,没有太多的实际意义。

添加远端实现SelfManagerService.java

我们都知道像系统里的服务都是放在frameworks/base/services/core/java/com/android/server目录下的,比如:

  1. ActivityManagerService.java 
    frameworks/base/services/core/java/com/android/server/am

  2. WindowManagerService.java 
    frameworks/base/services/core/java/com/android/server/wm

这里,我们没有特殊的需求就放在frameworks/base/services/core/java/com/android/server里

SelfManagerService.java

  1. package com.android.server;
  2. import android.util.Log;
  3. import android.os.ISelfManager;
  4. public class SelfManagerService extends ISelfManager.Stub {
  5. private static final String TAG = "SelfManagerService";
  6. public int selfAddNumber(int numberFirst, int numberSecond) {
  7. Log.d(TAG,"I WILL CALCULATE IT numberFirst is:"+numberFirst+" numberSecond is :"+numberSecond);
  8. return numberFirst + numberSecond;
  9. }
  10. public String selfAddString(String originalStr) {
  11. Log.d(TAG,"I will add a string to originalStr previous....");
  12. return "previous lalala "+originalStr;
  13. }
  14. }
  • 创建对应的SelfManager

/frameworks/base/core/java/android/app/SelfManager.java

  1. package android.app;
  2. import android.util.Log;
  3. import android.os.ISelfManager;
  4. import android.content.Context;
  5. import android.os.RemoteException;
  6. public class SelfManager {
  7. private static String TAG = "SelfManager";
  8. ISelfManager mSelfManager;
  9. public SelfManager(Context ctx,ISelfManager selfManager) {
  10. mSelfManager = selfManager;
  11. }
  12. public int selfAddNumber(int numberFirst, int numberSecond) throws RemoteException {
  13. Log.d(TAG,"SelfManager selfAddNumber ......... ");
  14. return mSelfManager.selfAddNumber(numberFirst,numberSecond);
  15. }
  16. public String selfAddString(String originalStr) throws RemoteException {
  17. Log.d(TAG,"SelfManager selfAddString .........");
  18. return mSelfManager.selfAddString(originalStr);
  19. }
  20. }
  • 管理和注册SelfManagerService

在开始之前,已经分析系统Service的注册流程,需要分别在SystemServer.java和SystemServiceRegistry.java中修改

  • 在SystemServer.java中将SelfManagerService添加到ServiceManager中管理,这里我添加到了startOtherServices方法中
  1. private void startOtherServices() {
  2. ....
  3. ServiceManager.addService("selfservice", new SelfManagerService());
  4. ....
  5. }
  • 在SystemServiceRegistry.java中注册我们的SelfManagerService服务
  1. import android.os.ISelfManager;
  2. static {
  3. ....
  4. registerService("selfservice", SelfManager.class,
  5. new CachedServiceFetcher<SelfManager>() {
  6. @Override
  7. public SelfManager createService(ContextImpl ctx) {
  8. IBinder b = ServiceManager.getService("selfservice");
  9. ISelfManager service = ISelfManager.Stub.asInterface(b);
  10. return new SelfManager(ctx,service);
  11. }});
  12. ....
  13. }
  • 这里的”selfservice”,一般来说需要在Context.java中声明,然后在这里引用,比如系统中的其他服务调用时候的应用,都是在Context.java中定义的,比如 
    这里写图片描述

这一步,别忘记引包就行.

更新sepolicy配置

另外还需要更新sepolicy的配置,否则即使编译通过也是不会生效的。

更新service.te文件

service.te主要用来定义我们自己服务的类型,在/system/sepolicy/service.te目录下,不同厂商的定制可能导致该路径不同在该文件中已经定义了很多service类型,只需要照着画就行了。

  1. type wifi_service, app_api_service, system_server_service, service_manager_type;
  2. // 参照 wifi_service的定义添加自己的定义就行了
  3. type self_service, system_api_service, system_server_service, service_manager_type;
  • 更新service_contexts文件
selfservice     u:object_r:self_service:s0

现在万事已经具备了.全编就可以了,需要注意, 在全编之前,需要更新当前系统的API 
执行make update-api -j4,完成之后会生成自己添加的服务的API

/frameworks/base/api/current.txt

  1. public class SelfManager {
  2. ctor public SelfManager(android.content.Context, android.os.ISelfManager);
  3. method public int selfAddNumber(int, int) throws android.os.RemoteException;
  4. method public java.lang.String selfAddString(java.lang.String) throws android.os.RemoteException;
  5. }
  • 完成之后,执行make命令全编就可以了

验证SelfManagerService

自己独立编写一个应用,然后参照packages/apps/中的其他模块实现Android.mk,或者基于系统已有的应用来测试,这里我是在Mms中添加的

  1. import android.app.SelfManager;
  2. import android.os.RemoteException;
  3. try {
  4. SelfManager selfManager = (SelfManager) getSystemService("selfservice");
  5. int addResult = selfManager.selfAddNumber(3,4);
  6. Log.d("Servicehahatest","result is :"+addResult);
  7. String strResult = selfManager.selfAddString("HELLO WORLD...");
  8. Log.d("Servicehahatest","result is :"+strResult);
  9. } catch(RemoteException e) {
  10. Log.d("Servicehahatest","RemoteException happend .....e is :"+e.toString());
  11. }

此时运行效果如下: 
这里写图片描述

OK,到此为止,在Android N中添加系统服务就完成了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/272968
推荐阅读
相关标签
  

闽ICP备14008679号