http://note.youdao.com/noteshare?id=0971ee06760cefc93d5bc2739ca9be01&sub=A90B6C65BD3F4BF48D44F14ECE2E47B4
总体流程:
1.创建接口
2.创建接口实现类
3.创建接口操作类
4.注册操作类
5.系统添加服务
6.解决权限问题。
备注:其实主要是1、2、3步骤。
具体步骤:
1.定义aidl文件。生成相应java文件。位置:/frameworks/basecore/java/android/os/ISelfManager.aidl
package android.os;
interface ISelfManager {
int selfAddNumber(int numberFirst, int numberSecond);
String selfAddString(String originalStr);
}
2.添加aidl文件到mk中,位置:frameworks/base/Android.mk已供编译:
LOCAL_SRC_FILES += \
core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \
core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl \
.....
core/java/android/os/ISelfManager.aidl \
3.编译生成接口。后面编译生成也可以。这里主要是逻辑上好理解,我先生成接口,在对接口进行实现和操作。
mmm frameworks/base
4.添加服务,已实现刚刚编译的接口,位置:frameworks/base/services/core/java/com/android/server
package com.android.server; import android.util.Log; import android.os.ISelfManager; public class SelfManagerService extends ISelfManager.Stub { private static final String TAG = "SelfManagerService"; public int selfAddNumber(int numberFirst, int numberSecond) { Log.d(TAG,"I WILL CALCULATE IT numberFirst is:"+numberFirst+" numberSecond is :"+numberSecond); return numberFirst + numberSecond; } public String selfAddString(String originalStr) { Log.d(TAG,"I will add a string to originalStr previous...."); return "previous lalala "+originalStr; } }
5.添加操作类:位置:/frameworks/base/core/java/android/app/SelfManager.java
package android.app; import android.util.Log; import android.os.ISelfManager; import android.content.Context; import android.os.RemoteException; public class SelfManager { private static String TAG = "SelfManager"; ISelfManager mSelfManager; public SelfManager(Context ctx,ISelfManager selfManager) { mSelfManager = selfManager; } public int selfAddNumber(int numberFirst, int numberSecond) throws RemoteException { Log.d(TAG,"SelfManager selfAddNumber ......... "); return mSelfManager.selfAddNumber(numberFirst,numberSecond); } public String selfAddString(String originalStr) throws RemoteException { Log.d(TAG,"SelfManager selfAddString ........."); return mSelfManager.selfAddString(originalStr); } }
6.注册类:位置:/frameworks/base/core/java/android/app/SystemServiceRegistry.java 类中
import android.os.ISelfManager; static { .... registerService("selfservice", SelfManager.class, new CachedServiceFetcher
7.添加服务到系统:位置:/frameworks/base/services/java/com/android/server/SystemServer.java 的startOtherServices 方法中,注意:可能这个方法会有权限异常,最好加入try catch。
private void startOtherServices() { .... ServiceManager.addService("selfservice", new SelfManagerService()); .... }
备注:6.和 7 步骤中的“selfservice”一般在Context.java 中声明
8.权限问题:SELinux : avc: denied { add } for service ...错误
a.方法一:(记得替换相关img如果是模拟器替换掉ramdisk.img)
(1)添加服务类型:位置:/system/sepolicy/service.te
type self_service, system_api_service, system_server_service, service_manager_type;
(2)对服务添加权限:位置:/system/sepolicy/service_contexts
selfservice u:object_r:self_service:s0
(3)解决 SELinux : avc: denied { find } /system/sepolicy/untrusted_app.te
allow untrusted_app self_service:service_manager find;
b.手动关闭selinux:adb shell -> setenforce 0
9.编译:
(1)执行:make update-api -j4 完成之后会生成自己添加的服务的API 位置:/frameworks/base/api/current.txt
public class SelfManager { ctor public SelfManager(android.content.Context, android.os.ISelfManager); method public int selfAddNumber(int, int) throws android.os.RemoteException; method public java.lang.String selfAddString(java.lang.String) throws android.os.RemoteException; }
(2)make -j4
验证:
1.在android studio项目中引入,或创建SelfManager.java 注意包名为android.app。其实充当影子作用。
package android.app;
import android.os.RemoteException;
public class SelfManager {
public int selfAddNumber(int numberFirst, int numberSecond) throws RemoteException {
return 0;
}
public String selfAddString(String originalStr) throws RemoteException {
return "";
}
}
2.使用:
try {
SelfManager selfManager = (SelfManager) getSystemService("selfservice");
int addResult = selfManager.selfAddNumber(3,4);
Log.d("Servicehahatest","result is :"+addResult);
String strResult = selfManager.selfAddString("HELLO WORLD...");
Log.d("Servicehahatest","result is :"+strResult);
} catch(RemoteException e) {
Log.d("Servicehahatest","RemoteException happend .....e is :"+e.toString());
}
3.运行结果:
result is :7
等等其他日志