从jni

时间:2015-05-11 08:10:44

标签: java android java-native-interface

我正在尝试制作一个 android 应用,我需要在 jni 中访问加速度计。

java 代码:

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);         
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

jni 代码:

jclass cls_context = env->FindClass("android/content/Context");
jfieldID tService = env->GetStaticFieldID(cls_context, "SENSOR_SERVICE","Ljava/lang/String;");
jstring jstr = (jstring)env->GetStaticObjectField(cls_context, tService);

//getSystemService(SENSOR_SERVICE);
jclass cls_act = env->GetObjectClass(activity);
jmethodID GetSystemService = env->GetMethodID(cls_act,"getSystemService","(Ljava/lang/String;)Ljava/lang/Object;");
jobject systemservice = env->CallObjectMethod(activity,GetSystemService,jstr);

//getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
jclass sensormanager = env->GetObjectClass(systemservice);
jmethodID getdefaultsensor = env->GetMethodID(sensormanager, "getDefaultSensor", "(I)Landroid/hardware/Sensor;");
jobject sensor = env->CallObjectMethod(systemservice, getdefaultsensor, "Sensor.TYPE__ACCELEROMETER");

但是jobject sensor正在获得null

1 个答案:

答案 0 :(得分:0)

1是int类型的内联常量,值为$data = "<table> <tr><th>First Name</th><th>Last Name</th><th>GPA</th></tr> <tr><td>Bob</td><td>Jones</td><td>3.52</td></tr> <tr><td>Fred</td><td>Smith</td><td>2.89</td></tr> </table>"; ,应该在那里传递。

但无论如何,分成9行JNI代码的那两行看起来很糟糕。留下那些原始的Java代码然后从JNI调用它不是更好吗?