有没有办法在Android中检索多个传感器数据
我正在尝试获取加速度计和方向数据。目前,我创建了 AccelerometerManager 和 OrientationManager。他们都做同样的事情;实现 SensorEventListener 并从 onSensorChanged() 侦听器中的 value[] 检索数据。
有没有更简单的方法来做到这一点?看起来有 2 个带有重复代码的处理程序是没有必要的。有没有办法访问带有组合加速度计和方向数据的values[]数组?
由于有 2 个处理程序,我还必须这样做:
orientationManager = new OrientationManager(this);
orientationSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
orientationSensorManager.registerListener(orientationManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
accelerometerManager = new AccelerometerManager(this);
accelerometerSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensorManager.registerListener(accelerometerManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
I'm trying to get both accelerometer and orientation data. Currently, I created an AccelerometerManager and OrientationManager. They both do the same thing; implement SensorEventListener and retrieve data from values[] in onSensorChanged() listeners.
Is there an easier way to do this? It seems like having 2 handlers with duplicate code is uneccessary. Is there a way to access a values[] array with the combined accelerometer and orientation data together?
Because of the 2 handlers, I'm also having to do this:
orientationManager = new OrientationManager(this);
orientationSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
orientationSensorManager.registerListener(orientationManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
accelerometerManager = new AccelerometerManager(this);
accelerometerSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensorManager.registerListener(accelerometerManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能尝试这样的事情吗:
Can you try something like this: