我正在尝试制作一个
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传感器变得无效.