android 10 添加系统服务步骤

1.编写.aidl文件
存放位置:frameworks/base/core/java/android/os

package android.os;
interface ITEST {   
    //此处注意 如果参数中有数组类型记得加入out 和in关键字
    // int test(in byte【】 a)
    void test();
}

---------------------------------------------------------------------------------------------------------------------------------
2、将.aidl文件添加到 frameworks/base/Android.mk下的 LOCAL_SRC_FILES
(此处先make update-api生成对应的文件 Frameworks/base/api/current.txt中查看是否存在对应ITEST接口)
注意:Android 9以上是添加到frameworks/base/ Android.bp
存放位置:framework/base

java_defaults {
    name: "framework-defaults",
    installable: true,
    srcs: [
         ...省略
        "core/java/android/os/ITEST.aidl",
        ...省略

---------------------------------------------------------------------------------------------------------------------------------
3、Context.java添加服务注册名称, 添加该服务名称, 用于快捷注册和快捷引用
修改位置:frameworks/base/core/java/android/content/

  /**
     * Use with {@link #getSystemService(String)} to retrieve a
     * {@link android.app.TestManager} for controlling power management,
     * including "wake locks," which let you keep the device on while
     * you're running long tasks.
     */
 public static final String ITEST_SERVICE = "itest";

/** @hide */
    @StringDef(suffix = { "_SERVICE" }, value = {
            POWER_SERVICE,
            ITEST_SERVICE 
       ....,}

---------------------------------------------------------------------------------------------------------------------------------

4、新建TestService.java和TestManager.java
存放位置:frameworks\base\services\core\java\com\android\server\TestService.java
frameworks\base\core\java\android\app\TestManager.java

//TestManager 类
package android.app;
import android.content.Context;
import android.os.IFan;
import android.os.RemoteException;
import android.annotation.SystemService;

@SystemService(Context.ITEST_SERVICE )
public class TestManager {
    public static final String TAG = "TestManager";

    private Context mContext;
    private TestService mService;

    public FanManager(Context mContext, TestService mService) {
        this.mContext = mContext;
        this.mService = mService;
    }
    pubic void test(){
       try {
               mService.test();
             } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            }
    }
}


//TestService 类
package com.android.server;
import android.app.TestManager;
import android.content.Context;
import android.os.ITEST;
import android.os.RemoteException;
import com.android.server.SystemService;
import com.android.internal.app.IAppOpsService;

public class TestService extends SystemService {
    private Context mContext;
    private IAppOpsService mAppOps;
    private TestManager mManager;
    public TestService(Context context) {
        super(context);
        this.mContext = context;
    }
    public void systemReady(IAppOpsService appOps) {
      //TODO
    }
    @Override
    public void onStart() {
        publishBinderService(Context.ITEST_SERVICE, new BinderService());
    }
    private final class BinderService extends ITEST.Stub {
        @Override
        public void test() throws RemoteException {
           //TODO 具体操作在这里面实现
        }
    }
}

---------------------------------------------------------------------------------------------------------------------------------

5、SystemServer.java 中注册该service
修改位置: frameworks\base\services\java\com\android\server

import com.android.server.TestService ;//导包

private TestService  mTestService; //定义

 在SystemServer.java 中的startBootstrapServices()方法添加

 traceBeginAndSlog("StartTestManager");
 mTestService= mSystemServiceManager.startService(TestService.class);
 traceEnd();

traceBeginAndSlog("MakeTestManagerServiceReady");
 try {
            // TODO: use boot phase
  mTestService.systemReady(mActivityManagerService.getAppOpsService());
      } catch (Throwable e) {
   reportWtf("making Test Manager Service ready", e);
      }
 traceEnd();

---------------------------------------------------------------------------------------------------------------------------------

6、SystemServiceRegistry的static{}, 并在其中注册该service
修改位置:frameworks\base\core\java\android\app
import android.os.ITEST;//导包
//TestManager在同一目录下所以不用导包

   registerService(Context.ITEST_SERVICE, TestManager.class,ew CachedServiceFetcher() {
     @Override
     public I2CManager createService(ContextImpl ctx)throws ServiceNotFoundException {
                        IBinder b = ServiceManager.getServiceOrThrow(            Context.ITEST_SERVICE);
                        return new TestManager(
                                ctx.getOuterContext(),ITEST.Stub.asInterface(b));
        }});


以上步骤完成我们的自定义系统服务就完成了90%   但是我们还有最后一步,也是最重要的一步:

---------------------------------------------------------------------------------------------------------------------------------

7、 设置selinux规则
然后我们只需要添加这个自定义服务TestService相关的 SELinux 规则。为了方便之后验证,打开selinux
要记住这个命令  adb shell setenforce 1   # 1为打开  #0为关闭
Android 10 的 selinux 规则是放在 system/sepolicy 目录下的:
 service.te 和 service_contexts 都要加上 TestService的配置:

./prebuilts/api/27.0/public/service.te   # 需要
./prebuilts/api/28.0/public/service.te   # 需要
./prebuilts/api/29.0/public/service.te    # 需要
./prebuilts/api/26.0/public/service.te   # 需要
./public/service.te                 # 需要


./prebuilts/api/27.0/private/service_contexts   # 需要
./prebuilts/api/28.0/private/service_contexts   # 需要
./prebuilts/api/29.0/private/service_contexts       # 需要
./prebuilts/api/26.0/private/service_contexts   # 需要
./private/service_contexts         # 需要


在上述文件中
service_contexts文件添加配置 itest   u:object_r:itest_service:s0
service.te  中添加type  itest_service , system_server_service, service_manager_type
总结出一个规律 :就是service.te 文件在public目录下需要添加,private目录下不需要
                           service_contexts文件在private目录下的需要添加,public目录不需要
到此系统服务已经添加完成
itest                                    u:object_r:itest_service:s0

---------------------------------------------------------------------------------------------------------------------------------
8、验证:
    编译完成后,输入adb shell
                           #service  list 
    查看服务列表中 是否存在有添加服务 :itest        
    如果不存在 逐步排查 参照上一步看哪一步错误
    如果存在 就在代码中验证
    找到编译最新生成的class.jar文件,导入Androidstudio(如何导入自行百度)
   编译后的class.jar目录\out\target\common\obj\JAVA_LIBRARIES\framework_intermediates

  如果未生成  执行下make javac-check-framework 这个命令 就会生成!!!

觉得我写的好的兄弟 动动你发财的小手 点个赞 !!!

你们的认同将是我继续写下去的动力 !!

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值