1.在frameworks/base/core/java/android/app/下添加aidl文件
frameworks/base/core/java/android/app/IGMyTestService.aidl
package android.app; // Declare any non-default types here with import statements interface IGMyTestService { void setCmdData(int cmd,String data); void getCmdData(int cmd,String data); }
2.修改frameworks/base/Android.mk文件,将aidl加到该文件中,这样才可以被编译到,否则编译不过
将core/java/android/app/IGMyTestService.aidl \
加到LOCAL_SRC_FILES后面
3.在frameworks/base/services/core/java/com/android/server/下添加service文件
frameworks/base/services/core/java/com/android/server/ GMyTestManagerService.java
package com.android.server;
/**
* Created by Administrator on 2018/6/6.
*/
import android.app.*;
import android.app.IGMyTestService;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
public class GMyTestManagerService extends IGMyTestService.Stub {
private static final String TAG = "GMyTestManagerService";
private Context mContext = null;
public GMyTestManagerService(Context context) {
mContext = context;
initNative();
}
@Override
public void setCmdData(int cmd) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "setCmdData cmd"+cmd);
setCmdDataNative(cmd);
}
@Override
public void setCmdData(int cmd, int data1) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "setCmdData cmd"+cmd);
setCmdDataNative(cmd,data1);
}
@Override
public void setCmdData(int cmd, int data1,int data2) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "setCmdData cmd"+cmd);
setCmdDataNative(cmd,data1,data2);
}
@Override
public void setCmdData(int cmd, int data1,int data2,int data3) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "setCmdData cmd"+cmd);
setCmdDataNative(cmd,data1,data2,data3);
}
@Override
public void getCmdData(int cmd) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "getCmdData cmd"+cmd);
getCmdDataNative(cmd);
}
@Override
public void getCmdData(int cmd, int data1) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "getCmdData cmd"+cmd);
getCmdDataNative(cmd,data1);
}
@Override
public void getCmdData(int cmd, int data1,int data2) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "getCmdData cmd"+cmd);
getCmdDataNative(cmd,data1,data2);
}
@Override
public void getCmdData(int cmd, int data1,int data2,int data3) throws RemoteException {
// TODO Auto-generated method stub
Log.d(TAG, "getCmdData cmd"+cmd);
getCmdDataNative(cmd,data1,data2,data3);
}
public static native int initNative();
public static native void setCmdDataNative(int cmd);
public static native void setCmdOneDataNative(int cmd,int data1);
public static native void setCmdTwoDataNative(int cmd,int data1,int data2);
public static native void setCmdThreeDataNative(int cmd,int data1,int data2,int data3);
public static native int getCmdDataNative(int cmd);
public static native int getCmdOneDataNative(int cmd,int data1);
public static native int getCmdTwoDataNative(int cmd,int data1,int data2);
public static native int getCmdThreeDataNative(int cmd,int data1,int data2,int data3);
}
4.在frameworks/base/core/java/android/app/下添加manager给上层api
frameworks/base/core/java/android/app/ GMyTestManager.java
package android.app;
/**
* Created by Administrator on 2018/6/6.
*/
import android.content.Context;
import android.os.RemoteException;
public class GMyTestManager {
private static final String TAG = "GMyTestManager";
private IGMyTestService mService = null;
private Context mContext;
GMyTestManager(IGMyTestService service, Context context) {
mService = service;
mContext = context;
}
public void setCmdData(int cmd) {
try {
mService.setCmdData(cmd);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void setCmdData(int cmd, int data1) {
try {
mService.setCmdOneData(cmd, data1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void setCmdData(int cmd, int data1,int data2) {
try {
mService.setCmdTwoData(cmd, data1,data2);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void setCmdData(int cmd, int data1,int data2,int data3) {
try {
mService.setCmdThreeData(cmd, data1,data2,data3);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void getCmdData(int cmd) {
try {
mService.getCmdData(cmd);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void getCmdData(int cmd, int data1) {
try {
mService.getCmdOneData(cmd, data1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void getCmdData(int cmd, int data1,int data2) {
try {
mService.getCmdTwoData(cmd, data1,data2);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void getCmdData(int cmd, int data1,int data2,int data3) {
try {
mService.getCmdThreeData(cmd, data1,data2,data3);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
5.在frameworks/base/core/java/android/content/Context.java中添加service名称定义
public static final String GMYTEST_MANAGER_SERVICE = "gmyTestdevices";
6.在frameworks/base/services/java/com/android/server/SystemServer.java中添加服务
traceBeginAndSlog("StartGMyTestManagerService");
myTestService = new GMyTestManagerService(context);
ServiceManager.addService(Context.GMYTEST_MANAGER_SERVICE, myTestService);
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
7.在frameworks/base/core/java/android/app/ContextImpl.java中注册服务,此注册目前在android7.0的contextImple.java子类中SystemServiceRegistry.java.
registerService(Context.GMYTEST_MANAGER_SERVICE, GMyTestManager.class,
new CachedServiceFetcher
@Override
public GMyTestManager createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(Context.GMYTEST_MANAGER_SERVICE);
IGMyTestService service = IGMyTestService.Stub.asInterface(b);
return new GMyTestManager(service,ctx);
}});
如果需要访问hal层的jni调用,需要配置访问权限
8. 另外还需要更新sepolicy的配置,否则即使编译通过也是不会生效的.
service.te主要用来定义我们自己服务的类型,在/system/sepolicy/service.te目录下,不同厂商的定制可能导致该路径不同在该文件中已经定义了很多service类型.
type wifi_service, app_api_service, system_server_service, service_manager_type;
// 参照 wifi_service的定义添加自己的定义就行了
type gmyTestdevices_service, system_api_service, system_server_service, service_manager_type;
然后修改system/sepolicy/service_contexts文件
gmyTestdevices u:object_r:gmyTestdevices_service:s0
9.jni文件编写,在目录frameworks/base/services/core/jni下的com_android_server_GMyTestManagerService.cpp文件中
#define LOG_TAG "GMyTestManagerServiceJNI"
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace android
{
static int jniData = 1;
static jint devInit(JNIEnv* env, jobject clazz)
{
//ALOGI("Init dev_init ......");
YdManager::getInstance();
return 1;
}
static void devSetCmdData(JNIEnv* env, jobject clazz, jint cmd)
{
jniData = cmd;
YdManager &mgr(YdManager::getInstance());
mgr.exeCmdSet(cmd);
ALOGI("dev_setCmdData......jniData=%d\n",cmd);
}
static void devSetCmdOneData(JNIEnv* env, jobject clazz, jint cmd,jint data1)
{
jniData = cmd;
YdManager &mgr(YdManager::getInstance());
mgr.exeCmdSet(CMD_GPIO_NOPEN);
ALOGI("dev_setCmdData......jniData=%d\n",cmd);
}
static void devSetCmdTwoData(JNIEnv* env, jobject clazz, jint cmd,jint data1,jint data2)
{
jniData = cmd;
YdManager &mgr(YdManager::getInstance());
mgr.exeCmdSet(CMD_GPIO_NOPEN);
ALOGI("dev_setCmdData......jniData=%d\n",cmd);
}
static void devSetCmdThreeData(JNIEnv* env, jobject clazz, jint cmd,jint data1,jint data2,jint data3)
{
jniData = cmd;
YdManager &mgr(YdManager::getInstance());
mgr.exeCmdSet(CMD_GPIO_NOPEN);
ALOGI("dev_setCmdData......jniData=%d\n",cmd);
}
static jint devGetCmdData(JNIEnv* env, jobject clazz,jint cmd)
{
ALOGI("dev_getCmdData......jniData=%d\n",cmd);
return jniData;
}
static jint devGetCmdOneData(JNIEnv* env, jobject clazz,jint cmd,jint data1)
{
ALOGI("dev_getCmdData......jniData=%d\n",cmd);
return jniData;
}
static jint devGetCmdTwoData(JNIEnv* env, jobject clazz,jint cmd,jint data1,jint data2)
{
ALOGI("dev_getCmdData......jniData=%d\n",cmd);
return jniData;
}
static jint devGetCmdThreeData(JNIEnv* env, jobject clazz,jint cmd,jint data1,jint data2,jint data3)
{
ALOGI("dev_getCmdData......jniData=%d\n",cmd);
return jniData;
}
/*Java本地接口方法表*/
static const JNINativeMethod method_table[] = {
{"initNative", "()I",(void*)devInit},
{"setCmdDataNative", "(I)V",(void*)devSetCmdData},
{"setCmdOneDataNative", "(II)V",(void*)devSetCmdOneData},
{"setCmdTwoDataNative", "(III)V",(void*)devSetCmdTwoData},
{"setCmdThreeDataNative", "(IIII)V",(void*)devSetCmdThreeData},
{"getCmdDataNative", "(I)I",(void*)devGetCmdData},
{"getCmdOneDataNative", "(II)I",(void*)devGetCmdOneData},
{"getCmdTwoDataNative", "(III)I",(void*)devGetCmdTwoData},
{"getCmdThreeDataNative", "(IIII)I",(void*)devGetCmdThreeData},
};
/*注册Java本地接口方法*/
int register_android_server_GMyTestManagerService(JNIEnv *env)
{
return jniRegisterNativeMethods(env, "com/android/server/GMyTestManagerService", method_table, NELEM(method_table));
}
};
10. 这个JNI的cpp文件写好后,就要在\frameworks\base\services\core\jni\onload.cpp文件中添加注册函数, 这个onload.cpp文件实现在libandroid_servers模块中,当系统加载libandroid_servers模块时,就会调用实现在onload.cpp文件中的JNI_OnLoad函数,这样就把前面定义的三个JNI方法注册到Java虚拟机中去了。
namespace android {
int register_android_server_GMyTestManagerService (JNIEnv *env);
};
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
register_android_server_GMyTestManagerService(env);
return JNI_VERSION_1_4;
}
11.将开发好的cpp的jni文件在目录下/frameworks/base/services/core/jni/Android.mk中进行编译.
$(LOCAL_REL_DIR)/com_android_server_GMyTestManagerService.cpp \