获取传感器事件.值.长度()(无需等待 onSensorChanged())



我需要以编程方式查找所有传感器的event.values.length()。所以我正在做的是,注册所有传感器以获得每个传感器的event.values.length()。对于大多数传感器,onSensorChanged()在短时间内被调用。但对于其他一些传感器,设备需要处于运动状态。然后只会触发onSensorChanged()

我找到了这个官方文档。但是有没有办法以编程方式找到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();
     }
 }

最新更新