JNI访问Java中各方法

不啰嗦直接上代码(在这里贴一部分代码,如果要源码,点击最后的连接去下载)

1.eclipse使用.dll库调用

/*

返回字符给java层

*/

JNIEXPORT jstring JNICALL Java_com_zack_jni_JniDemo_getStringJniOne

(JNIEnv * env, jclass jclz){

return (*env)->NewStringUTF(env, "hello Wrold");

}

/* 返回一个不是static 的*/

JNIEXPORT jstring JNICALL Java_com_zack_jni_JniDemo_getNoStaticString

(JNIEnv * env, jobject jobj){

return (*env)->NewStringUTF(env, "no static ");

}

/* 通过 方法来改变 里面的的值*/

JNIEXPORT jstring JNICALL Java_com_zack_jni_JniDemo_accessField

(JNIEnv * env, jobject jobj){

jclass jcl = (*env)->GetObjectClass(env, jobj);

jfieldID fid = (*env)->GetFieldID(env, jcl, "key", "Ljava/lang/String;");

// 得到key 对应的值

// GetXXXField

jstring jstr=(*env)->GetObjectField(env, jobj, fid);

// jni -> c

char * c_str = (*env)->GetStringUTFChars(env, jstr, NULL);

char tempString[30] = "zack";

strcat(tempString, c_str);

jstring newString = (*env)->NewStringUTF(env, tempString);

(*env)->SetObjectField(env, jobj, fid, newString);

(*env)->ReleaseStringUTFChars(env, newString, c_str);

return newString;

//// jclass

//jclass jclz = (*env)->GetObjectClass(env, jobj);

//// fieldId 属性名称,属性签名

//jfieldID fid = (*env)->GetFieldID(env, jclz, "key", "Ljava/lang/String;");

//// key -> dongNao key

//// 得到key 对应的值

//// GetXXXField

//jstring jstr = (*env)->GetObjectField(env, jobj, fid);

//// jni -> c

//char * c_str = (*env)->GetStringUTFChars(env, jstr, NULL);

//char text[30] = "dongNao";

//// 生成新的字符串 dongNao key

//strcat(text, c_str);

//// C -> jni

//jstring new_str = (*env)->NewStringUTF(env, text);

////setXXXField

//(*env)->SetObjectField(env, jobj, fid, new_str);

//(*env)->ReleaseStringChars(env, new_str, c_str);

//return new_str;

}

/* 改变 java 成的静态值 */

JNIEXPORT void JNICALL Java_com_zack_jni_JniDemo_accessStaticField

(JNIEnv * env, jobject jobj){

jclass jcl = (*env)->GetObjectClass(env, jobj);

jfieldID fid=(*env)->GetStaticFieldID(env, jcl, "count", "I");

jint count = (*env)->GetStaticIntField(env, jobj, fid);

count++;

(*env)->SetStaticIntField(env, jcl, fid, count);

}

// 中文的处理

JNIEXPORT jobject JNICALL Java_com_zack_jni_JniDemo_chineseChars

(JNIEnv * env, jobject jobj,jstring in){

}

AS 动态注册:

static  const JNINativeMethod getMethod[]= {

{

"diff", "(Ljava/lang/String;Ljava/lang/String;I)V", (void *) native_zack_diff

}

,{

"getString","()Ljava/lang/String;",(void*)native_java_str

}

,{

"getNoStaticString","()Ljava/lang/String;",(void*)native_no_static

},

{

"accessField","()Ljava/lang/String;",(void*)change_zack_key

},

{

"chineseChars","(Ljava/lang/String;)Ljava/lang/String;",(void*)getchineseChars

},

{

"acceessConstructor","()Ljava/util/Date;",(void*)zack_getDate

},

{

"getStudentName","()Ljava/lang/String;",(void*)zack_getStudentName

}

};

static int registerNatives(JNIEnv* engv)

{

LOGI("registerNatives begin");

jclass  clazz;

clazz = (*engv) -> FindClass(engv, "com/zack/banner/FileUtils");

if (clazz == NULL) {

LOGI("clazz is null");

return JNI_FALSE;

}

if ((*engv) ->RegisterNatives(engv, clazz, getMethod, NELEM(getMethod)) < 0) {

LOGI("RegisterNatives error");

return JNI_FALSE;

}

return JNI_TRUE;

}

//JNI_OnLoad

JNIEXPORT jint JNI_OnLoad(JavaVM *vm,void* reserved){

LOGI("jni_OnLoad begin");

JNIEnv* env = NULL;

if ((*vm)->GetEnv(vm,(void**) &env, JNI_VERSION_1_4) != JNI_OK) {

LOGI("ERROR: GetEnv failed\n");

return -1;

}

assert(env != NULL);

registerNatives(env);

return  JNI_VERSION_1_4;

}

#include "com_zack_banner_FileUtils.h"#include#include#include "assert.h"#include#define  TAG "zack"

#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)

# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))

//ava_com_zack_banner_FileUtils_diff

JNIEXPORT void JNICALL native_zack_diff

(JNIEnv * env, jclass jcl, jstring path, jstring pattern_Path, jint file_num) {

LOGI("jni begin");

}

JNIEXPORT jstring JNICALL native_java_str(JNIEnv * env,jclass jcl){

return  (*env)->NewStringUTF(env,"hello zack  static Method");

}

JNIEXPORT jstring JNICALL native_no_static(JNIEnv * env,jobject jobj){

return  (*env)->NewStringUTF(env,"hello zack  not static Method");

}

/* 通过 方法来改变 里面的的值*/

JNIEXPORT jstring JNICALL change_zack_key

(JNIEnv * env, jobject jobj) {

jclass jcl = (*env)->GetObjectClass(env, jobj);

jfieldID fid = (*env)->GetFieldID(env, jcl, "key", "Ljava/lang/String;");

// 得到key 对应的值

// GetXXXField

jstring jstr = (*env)->GetObjectField(env, jobj, fid);

// jni -> c

char *c_str = (*env)->GetStringUTFChars(env, jstr, NULL);

char tempString[30] = "zack";

strcat(tempString, c_str);

jstring newString = (*env)->NewStringUTF(env, tempString);

(*env)->SetObjectField(env, jobj, fid, newString);

(*env)->ReleaseStringUTFChars(env, newString, c_str);

return newString;

}

源码

注意:Android  jni目录

你可能感兴趣的:(JNI访问Java中各方法)