我需要以编程方式找到所有传感器的 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();
}
}
关于android - 获取传感器 event.values.length()(无需等待 onSensorChanged()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459881/