简单的Jni 以及Aidl的实现

首先解释一下Jni,在我看来Jni 说白了就是Android系统中Java与Native交互的工具

那么AIDL又是什么呢,android是特有的AIDL通信机制,是为了让实现在其它进程里的代码也能调用某个接口。

然后说一下为什么要写这个Jni以及AIDL呢,这是因为当时遇到个问题,需要获取某个so里面的一个数据,该so必须通过jni来获取。具体为什么非要通过jni,这里不多说。

最后说一下基本思路。首先从C文件调用获取数据的方法,然后通过jni传给上层,上层通过aidi可以在各个进程实时获取数据

主要代码如下

\device\mediatek\common\sepolicy\full\service.te

# 这里添加一个系统服务
type illacommon_service, app_api_service, system_server_service, service_manager_type;

\device\mediatek\common\sepolicy\full\service_contexts

# add by illa 
illacommon    u:object_r:illacommon_service:s0

device\mediatek\common\sepolicy\full\system_server.te

# add by illa
allow system_server illacommon_service:service_manager add;

frameworks\base\services\core\java\com\android\server\illaCommonService.java

public class illaCommonService extends IillaCommonService.Stub {//add by illa

illaCommonService() {}




public int get_teekey_status(){
return get_teekey_status_native();
}

    private static native int get_teekey_status_native();

};

添加一个aidl文件

frameworks\base\core\java\android\os\IillaCommonService.aidl

interface IillaCommonService { 
//add by illa
int get_teekey_status();
//add by illa
}

\frameworks\base\services\core\jni\Android.mk

LOCAL_SRC_FILES += \

 $(LOCAL_REL_DIR)/com_android_server_illaCommonService.cpp \


LOCAL_SHARED_LIBRARIES += \  这里导入要使用的lib


frameworks\base\services\core\jni\com_android_server_illaCommonService.cpp

namespace android

struct tee_device_t* tee_device = NULL;

    static inline int tee_device_open(const hw_module_t* module, struct tee_device_t** device) {  
        return module->methods->open(module, TEE_HARDWARE_MODULE_ID, (struct hw_device_t**)device);  
    }

/**
which
1 sharp ic
2 elan ic
enable
true open the ic function
false close the ic function
Method to open or close the target ic
*/
    static jint enable(JNIEnv* env, jobject clazz, jint which, jboolean enable) { 
        int err = 3;
hw_module_t* module;  
ALOGD("tee JNI: 222222222initializing......");   
if (0 == hw_get_module(TEE_HARDWARE_MODULE_ID, (hw_module_t const**)&module)) {
   ALOGD("tee JNI: tee Stub found.");
   if(tee_device_open(module, &tee_device) == 0) {
       ALOGD("tee JNI: tee device is open.");
       err = tee_device->tee_en(tee_device, which, enable ? 1 : 0);
   }else{
ALOGD("tee JNI: failed to open tee device.");  
}
}else{
ALOGD("tee JNI: failed to get tee stub module.");
}
        return err;  
    }  
   
    static jint get_teekey_status(JNIEnv* env, jobject clazz) { 
        int nret = -1;
//hw_module_t* module;  
ALOGD("tee JNI: 11111111initializing......");   
//if (0 == hw_get_module(TEE_HARDWARE_MODULE_ID, (hw_module_t const**)&module)) {
   ALOGD("tee JNI: tee Stub found.");
   //if(tee_device_open(module, &tee_device) == 0) {
       ALOGD("tee JNI: tee device is open.");
       nret = verify_tee_all();
   //}else{
// ALOGD("tee JNI: failed to open tee device.");  
//}
//}else{
// ALOGD("tee JNI: failed to get tee stub module.");
//}

return nret;
    }    
    static const JNINativeMethod method_table[] = {
        {"enable_native", "(IZ)I", (void*)enable},
        {"get_teekey_status_native", "()I", (void*)get_teekey_status} 
    };    
  
    int register_com_android_server_illaCommonService(JNIEnv *env) {  
            return jniRegisterNativeMethods(env, "com/android/server/illaCommonService", method_table, NELEM(method_table));  
    }  


};



\frameworks\base\services\core\jni\onload.cpp


namespace android {

...

//add by gaoshifeng for tee test
int register_com_android_server_illaCommonService(JNIEnv *env);
//add by gaoshifeng for tee test

}


extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{

...

//add by gaoshifeng for tee test
register_com_android_server_illaCommonService(env);
//add by gaoshifeng for tee test
    return JNI_VERSION_1_4;
}



frameworks\base\services\java\com\android\server\SystemServer.java

            traceBeginAndSlog("StartVibratorService");
            vibrator = new VibratorService(context);
            ServiceManager.addService("vibrator", vibrator);
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
//add by gaoshifeng for tee test
Slog.i(TAG, "illacommon Service");
            ServiceManager.addService("illacommon", newillaCommonService());
//add by gaoshifeng for tee test


\frameworks\base\Android.mk


LOCAL_SRC_FILES += \

core/java/android/os/IillaCommonService.aidl \  

这里加入aidl的引用

java文件直接调用aidl 接口即可

try {
mTee = android.os.IillaCommonService.Stub.asInterface(android.os.ServiceManager.getService("illacommon")).get_teekey_status();
} catch (Exception e) {
e.printStackTrace();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值