Java – access accelerometer from JNI

I'm trying to make an Android application. I need to access the accelerometer in JNI

Java code:

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

JNI Code:

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");

However, the jobject sensor becomes invalid

Solution

Sensor. TYPE_ Accelerometer is an inline constant of type int with a value of 1, which should be passed there

But anyway, the two lines of JNI code divided into nine lines look terrible Wouldn't it be better to leave the original java code and call it from JNI?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>