获取传感器 event.values.length()(无需等待 onSensorChanged())
Get Sensor event.values.length() (without waiting for onSensorChanged())
我需要以编程方式找到所有传感器的 event.values.length()
。所以我正在做的是,注册所有传感器以获得每个传感器的 event.values.length()
。对于大多数传感器,onSensorChanged()
会在短时间内被调用。但对于其他一些传感器,设备需要处于运动状态。那么只有onSensorChanged()
会被触发。
I found this official doc. 但是有没有办法以编程方式找到 event.values.length()
。
我找到了找到传感器的方法 event.values.length
。不确定这是正确的方法,但确实有效。
Method[] methodList = Sensor.class.getDeclaredMethods();
int m_count = methodList.length;
for (int j = 0; j < m_count; j++) {
Method method = methodList[j];
if (!method.getName().equals("getMaxLengthValuesArray")) {
continue;
}
method.setAccessible(true);
try {
int values_length = (Integer) method.invoke(mSensor, mSensor, Build.VERSION.SDK_INT);
Log.e(TAG,"value length is "+values_length);
break;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
我需要以编程方式找到所有传感器的 event.values.length()
。所以我正在做的是,注册所有传感器以获得每个传感器的 event.values.length()
。对于大多数传感器,onSensorChanged()
会在短时间内被调用。但对于其他一些传感器,设备需要处于运动状态。那么只有onSensorChanged()
会被触发。
I found this official doc. 但是有没有办法以编程方式找到 event.values.length()
。
我找到了找到传感器的方法 event.values.length
。不确定这是正确的方法,但确实有效。
Method[] methodList = Sensor.class.getDeclaredMethods();
int m_count = methodList.length;
for (int j = 0; j < m_count; j++) {
Method method = methodList[j];
if (!method.getName().equals("getMaxLengthValuesArray")) {
continue;
}
method.setAccessible(true);
try {
int values_length = (Integer) method.invoke(mSensor, mSensor, Build.VERSION.SDK_INT);
Log.e(TAG,"value length is "+values_length);
break;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}