android 中java调用C++ 定义接口类
package test.simulateapp;
/**
* Created by zhu on 17-6-22.
*/
public class JniInfence {
public static native void init(JNICallJava jniCallJava,String user,String passwd,String addr,int port);
public static native void sendSet(int index,String str,String s);
static {
System.loadLibrary("native-lib");
}
}
通过javah编译生成对应C头文件,但这种方式由于函数名太长,一般修改还要编译 我不是很喜欢,就不介绍了,这里介绍的是JVM的隐式注册,JNI_OnLoad和JNI_OnUnload。在android 启动时会调用JNI_OnLoad函数通过他注册上面接口类的两个函数入口,使用如下:
jint JNI_OnLoad(JavaVM *vm,void *reserved);
void JNI_OnUnload(JavaVM *vm,void *reserved);
void InitParameter(JavaVM *vm);
void init(JNIEnv *env, jobject,jobject,jstring ip,jstring i,jstring p,jint);//
void send_set(JNIEnv *env, jobject,jint,jstring str,jstring ip);
#define FUNCTIONS (2)
JNINativeMethod ParameterSetFunctions[FUNCTIONS] = {
{ "init","(Ltest/simulateapp/JNICallJava;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",(void*) init },
{ "sendSet","(ILjava/lang/String;Ljava/lang/String;)V",(void*) send_set },
};
jint JNI_OnLoad(JavaVM *vm,void *reserved)
{
__android_log_print(ANDROID_LOG_ERROR,"JNI_OnLoad","JNI_OnLoad初始化[%d].", __LINE__);
__g_jvm = vm;
InitParameter(__g_jvm);
return JNI_VERSION_1_4;
}
void JNI_OnUnload(JavaVM *vm,void *reserved)
{
__android_log_print(ANDROID_LOG_ERROR,"JNI_OnUnload","JNI 结束 [%d].", __LINE__);
}
void InitParameter(JavaVM *vm)
{
JNIEnv* env = 0; //注册时在JNIEnv中实现的,所以必须首先获取它
if(vm->GetEnv( (void**)&env, JNI_VERSION_1_4) != JNI_OK) //从JavaVM获取JNIEnv,一般使用1.4的版本
return ;
jclass clazz = env->FindClass( "test/simulateapp/JniInfence"); //这里可以找到要注册的类,前提是这个类已经加载到java虚拟机中。 这里说明,动态库和有native方法的类之间,没有任何对应关系。
if(clazz == 0)
{
return ;
}
g_parameter_class = (jclass)env->NewGlobalRef(clazz);
if(env->RegisterNatives( g_parameter_class,ParameterSetFunctions, FUNCTIONS)!= JNI_OK) //这里就是关键了,把本地函数和一个java类方法关联起来。不管之前是否关联过,一律把之前的替换掉!
{
return ;
}
g_server_class = env->FindClass( "test/simulateapp/JNICallJava");
if(g_server_class == 0)
{
return ;
}
g_severeCid = env->GetMethodID(g_server_class, "update_data", "(IILjava/lang/String;I)V");//C调JAVA注册
if (g_severeCid == 0) {
__android_log_print(ANDROID_LOG_ERROR,"Android_Play",
"%s: could not get g_severeCid ID", __FUNCTION__);
}
return;
}
/* C++调JAVA */
void update_data(int index,int cmd,string ss,int strategy_id)
{
if (__g_jvm)
{
bool isAttached = false;
JNIEnv* env = NULL;
if (__g_jvm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
// try to attach the thread and get the env
// Attach this thread to JVM
jint res = __g_jvm->AttachCurrentThread(&env, NULL);
// Get the JNI env for this thread
if ((res < 0) || !env) {
__android_log_print(ANDROID_LOG_DEBUG,"Android_Play",
"%s: Could not attach thread to JVM (%d, %p)",
__FUNCTION__, res, env);
env = NULL;
} else {
isAttached = true;
}
}
if(env)
{
if (g_severeCid)
{
env->CallVoidMethod((jobject)g_call_java_handler, g_severeCid,index,cmd,env->NewStringUTF(ss.c_str()),strategy_id);
}
}
if (isAttached) {
if (__g_jvm->DetachCurrentThread() < 0) {
}
}
}
return;
}