#include #include#include#include
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
#define LOGII(...) ((void)__android_log_print(ANDROID_LOG_INFO, "the int value is : %d", __VA_ARGS__))JNIEXPORTvoidJNICALL
Java_com_ndk_NDKFirstActivity_acessField(JNIEnv*env , jobject obj)
{
jfieldID fid;
jstring jstr;const char*str;/**从对象中回去类*/jclass cls= (*env)->GetObjectClass(env ,obj);/**根据类、字段、和字段类型获取字段id*/fid= (*env)->GetFieldID(env ,cls,"str" ,"Ljava/lang/String;");//类型后面的分号不能省略
if(fid ==NULL)
{return;
}/**根据字段ID 和对象 获取值*/jstr= (*env)->GetObjectField(env ,obj,fid);
str= (*env)->GetStringUTFChars(env ,jstr ,NULL);if(str ==NULL)
{return;
}
LOGI(str);
(*env)->ReleaseStringUTFChars(env ,jstr,str);
jstr= (*env)->NewStringUTF(env ,"I reset the str value in native c,and success");if(jstr ==NULL)
{return;
}/**根据对象、字段id 设置值*/(*env)->SetObjectField(env ,obj , fid , jstr);
}
JNIEXPORTvoidJNICALL
Java_com_ndk_NDKFirstActivity_accessStaticField(JNIEnv*env ,jobject obj)
{
jfieldID fid ;
jint si;
jclass cls= (*env)->GetObjectClass(env , obj);
fid= (*env)->GetStaticFieldID(env ,cls ,"value","I");if(fid==NULL)
{return;
}
//根据类,设置静态字段值
si= (*env)->GetStaticIntField(env ,cls ,fid);//LOGI(si+"");
(*env)->SetStaticIntField(env ,cls ,fid ,2012);
}
JNIEXPORTvoidJNICALL
Java_com_ndk_NDKFirstActivity_invokeMethod(JNIEnv*env ,jobject obj)
{
jclass cls= (*env)->GetObjectClass(env ,obj);
jmethodID mid= (*env)->GetMethodID(env ,cls ,"callback","()V");if(mid ==NULL)
{return;
}
//更加对象、方法字段,调用 Java 方法
LOGI("i invoke callback in native c");
(*env)->CallVoidMethod(env ,obj ,mid);
}
JNIEXPORTvoidJNICALL
Java_com_ndk_NDKFirstActivity_invokeStaticMethod(JNIEnv*env , jobject obj)
{
jclass cls= (*env)->GetObjectClass(env , obj);
jmethodID mid=(*env)->GetStaticMethodID(env ,cls,"staticCallback","(I)V");if(mid ==NULL)
{return;
}
LOGI("i invoke staticCallback in native c");
jint ji= 1000;
//根据类,方法字段,调用静态方法
(*env)->CallStaticVoidMethod(env,cls,mid ,ji);
}
JNIEXPORT jobject JNICALL
Java_com_ndk_NDKFirstActivity_getData(JNIEnv*env , jobject obj , jint ji)
{
//根据类名,获取类
jclass dataClass= (*env)->FindClass(env ,"com/ndk/Data");if(dataClass ==NULL)
{return;
}
//根据类 获取方法字段
jmethodID cid= (*env)->GetMethodID(env ,dataClass ,"" ,"(I)V");if(cid==NULL)
{return;
}
LOGI("i init and return a construct in native c");
//根据类、 构造方法字段 初始化 类对象return (*env)->NewObject(env ,dataClass ,cid , ji);
}