ServiceManager addService流程(源码修改版)

1. 创建aidl文件

在源码frameworks/base/core/java/android/os/ 下面新增 一个 ICCCService.aidl

假设我们定义了5个函数,这些函数将会在SystemServer进程执行。

内容如下 

// ICCCService.aidl
package android.os;

// Declare any non-default types here with import statements

interface ICCCService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
   void setVal(String key,String value);
   String getVal(String key);
   void appendLog(String log);
   void clearLog();
   String readLog();
}

2. 创建Service文件 (CCCService)

在 frameworks/base/services/core/java/com/android/server/ 下面新增一个 CCCService.java 用来实现aidl文件定义的接口。

内容如下

package com.android.server;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.*;
import java.util.HashMap;
import android.os.RemoteException;
import android.os.ICCCService;


public class CCCService extends ICCCService.Stub {
    private static HashMap<String,String> map=new HashMap<>();
    private static String inner_log="";

    public CCCService() {

    }

    @Override
    public void setVal(String key, String value) throws RemoteException {
        map.put(key,value);
    }

    @Override
    public String getVal(String key) throws RemoteException {
        return map.get(key);
    }

    @Override
    public void appendLog(String log) throws RemoteException {
        inner_log+=log+"\n";
    }

    @Override
    public void clearLog() throws RemoteException {
        inner_log="";
    }

    @Override
    public String readLog() throws RemoteException {
        return inner_log;
    }

}

3. 将自定义Service 加入到SystemServer 启动进程

先在 frameworks/base/core/java/android/content/Context.java 中添加一行 

public static final String CCC_SERVICE="ccc";

修改 frameworks/base/services/java/com/android/server/SystemServer.java

在  startOtherServices() 函数 的try模块中增加以下代码

 try {

     Slog.i(TAG, "CCC Service");

     ServiceManager.addService(Context.CCC_SERVICE, new CCCService());

 } catch (Throwable e) {

      Slog.e(TAG, "Failure starting CCC Service", e);

 }

最终效果如图

4.  创建Manager,即CCCManager

在frameworks/base/core/java/android/app/ 下创建CCCManager.java 文件 内容如下

package android.app;

import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ICCCService;
import android.util.Log;

public class CCCManager {
    ICCCService mService;
    public CCCManager(Context ctx,ICCCService service){
        mService=service;
    }
    public void setVal(String key,String value){
        try{
            mService.setVal(key,value);
        }catch(Exception e){
            Log.e("CCCManager",e.toString());
            e.printStackTrace();
        }

    }
    public String getVal(String key){
        try{
            return mService.getVal(key);
        }catch(Exception e){
            Log.e("CCCManager",e.toString());
            e.printStackTrace();
        }
        return null;
    }
    public void appendLog(String log){
        try{
            mService.appendLog(log);
        }catch(Exception e){
            Log.e("CCCManager",e.toString());
            e.printStackTrace();
        }
    }
    public void clearLog(){
        try{
            mService.clearLog();
        }catch(Exception e){
            Log.e("CCCManager",e.toString());
            e.printStackTrace();
        }
    }
    public String readLog(){
        try{
            return mService.readLog();
        }catch(Exception e){
            Log.e("CCCManager",e.toString());
            e.printStackTrace();
        }
        return null;
    }
}

5. 注册到SystemService

修改frameworks/base/core/java/android/app/SystemServiceRegistry.java

在静态代码块中增加

 registerService(Context.CCC_SERVICE, CCCManager.class,
                new CachedServiceFetcher<CCCManager>() {
                    @Override
                    public CCCManager createService(ContextImpl ctx) {
                        IBinder b = ServiceManager.getService(Context.CCC_SERVICE);
                        ICCCService service = ICCCService.Stub.asInterface(b);
                        return new CCCManager(ctx, service);
                    }});

6.  修改SePolicy的编译验证

修改 /external/sepolicy/service.te

在最后一行添加

type ccc_service, system_api_service, system_server_service, service_manager_type;

然后修改同目录下 /external/sepolicy/service_contexts 文件

中间插入一行 

ccc u:object_r:ccc_service:s0

如图所示

7.  重新编译源码

别忘了先 make update-api。

8. 测试 

tip: 

可以先创建一个java工程简单写一个CCCManager类,导出jar 在ide中使用provided 依赖。这样开发过程中就不会报错。

java临时工程如下使用 IDEA创建的java项目

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yinhunzw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值