模拟两个类一个类中有静态的常量,另一个类是常量…
Java代码:
Test类
public class Test {
static {
System.loadLibrary("wechatcore");
}
public static double d1 = 0;
public static double d2 = 0;
//模拟一个函数进行JNI赋值操作...
public static native void t(int n1, int n2);
//这是Java代码...
public static void test(int n1, int n2) {
if (n1 == 1 && n2 == 2) {
Test1 test1=new Test1();
d1 = test1.lat;
d2 = test1.lng;
Log.e("test",d1+"----"+d2);
}
}
}
Test1类
public class Test1 {
public double lat =10.2;
public double lng =18.3;
}
在按钮点击事件调用Test中的native方法t
Test.t(1,2);
将Java代码转JNI代码:
/**
* 模拟操作...
*/
extern "C"
JNIEXPORT void JNICALL
Java_com_tencent_mm_wvs_test_Test_t(JNIEnv *env, jclass type, jint n1, jint n2) {
jdouble lat;
jdouble lng;
jdouble d1;
jdouble d2;
if (n1 == 1 && n2 == 2) {
LOGE("-----进入if true开始-----");
//获取test类...
jclass Test1 = env->FindClass("com/tencent/mm/wvs/test/Test1");
jmethodID test1_methodID = env->GetMethodID(Test1, "" , "()V");
//创建对象
jobject test = env->NewObject(Test1, test1_methodID);
//找到Test1中的两个值lat lng
jfieldID lat_fieldID = getFieldID(env, Test1, "lat", "D");
lat = env->GetDoubleField(test, lat_fieldID);
jfieldID lng_fieldID = getFieldID(env, Test1, "lng", "D");
lng = env->GetDoubleField(test, lng_fieldID);
//打印这两个值
LOGE("(Test1) lat = %f", lat);
LOGE("(Test1) lng = %f", lng);
//获取静态的d1,d2
jfieldID d1_fieldID = getStaticFieldID(env, type, "d1", "D");
d1 = env->GetStaticDoubleField(type, d1_fieldID);
jfieldID d2_fieldID = getStaticFieldID(env, type, "d2", "D");
d2 = env->GetStaticDoubleField(type, d2_fieldID);
//输出d1,d2的值
LOGE("(Test)初始值 d1 = %f", d1);
LOGE("(Test)初始值 d2 = %f", d2);
//赋值操作...
env->SetStaticDoubleField(type, d1_fieldID, lat);
env->SetStaticDoubleField(type, d2_fieldID, lng);
//获取赋值以后的值...
d1 = env->GetStaticDoubleField(type, d1_fieldID);
d2 = env->GetStaticDoubleField(type, d2_fieldID);
//打印赋值以后的值
LOGE("(Test)赋值后 d1 = %f", d1);
LOGE("(Test)赋值后 d2 = %f", d2);
LOGE("-----进入if true结尾-----");
}
}
运行结果:
12-25 11:57:57.099 21577-21577/com.tencent.mm.wvs E/捕获====: -----进入if true开始-----
12-25 11:57:57.099 21577-21577/com.tencent.mm.wvs E/捕获====: (Test1) lat = 10.200000
12-25 11:57:57.099 21577-21577/com.tencent.mm.wvs E/捕获====: (Test1) lng = 18.300000
12-25 11:57:57.099 21577-21577/com.tencent.mm.wvs E/捕获====: (Test)初始值 d1 = 0.000000
12-25 11:57:57.100 21577-21577/com.tencent.mm.wvs E/捕获====: (Test)初始值 d2 = 0.000000
12-25 11:57:57.100 21577-21577/com.tencent.mm.wvs E/捕获====: (Test)赋值后 d1 = 10.200000
12-25 11:57:57.100 21577-21577/com.tencent.mm.wvs E/捕获====: (Test)赋值后 d2 = 18.300000
12-25 11:57:57.100 21577-21577/com.tencent.mm.wvs E/捕获====: -----进入if true结尾-----