SensorManager:一个SensorEventListener VS多个侦听器 [英] SensorManager: One SensorEventListener VS Multiple Listeners

查看:119
本文介绍了SensorManager:一个SensorEventListener VS多个侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将传感器的测量结果记录到设备内存中.为此,我为许多传感器注册了相同的SensorEventListener,然后根据类型使用开关将它们分开.

例如

int type = sensor.getType();

switch (type) {
    case Sensor.TYPE_ACCELEROMETER:
    // log accelerometer data
    break;

case Sensor.TYPE_GYROSCOPE:
    // Log gyroscope data
    break;

...

我的问题是,考虑到我向SENSOR_DELAY_FASTEST注册了侦听器,最有效的方式(较少的电池电量消耗)是什么?每个传感器都有一个侦听器,或者只有一个侦听器并用开关将传感器分开?

非常感谢您

Minos

解决方案

当每个SensorEventListener创建其自己的FIFO缓冲区时,Android应用程序应为每个Sensor使用一个 SensorEventListener . SensorEvent . 流过芯片的电流的消耗水平与提供给传感器的延迟有关,而与传感器所附的SensorEventListener 无关.

如果将多个传感器用于 SENSOR_DELAY_FASTEST ,则由于缓冲区溢出,如果多个Sensor与一个SensorEventListener一起使用,则某些事件可能会丢失.

因此,为了获得最佳结果并确保不会丢失任何事件:

  • 为一个传感器使用一个SensorEventListener.

    要提高效率:

  • 在暂停应用程序时,请取消注册所有已注册的传感器,如果不需要,则即使关闭了应用程序,它们也不会从芯片上获取事件.

I'm trying to log sensor measurements to the device memory. For this, I register the same SensorEventListener for many sensors, and later separate them using a switch, based on the type.

E.g.

int type = sensor.getType();

switch (type) {
    case Sensor.TYPE_ACCELEROMETER:
    // log accelerometer data
    break;

case Sensor.TYPE_GYROSCOPE:
    // Log gyroscope data
    break;

...

My question is, what is the most efficient (less battery power consuming) way, having in mind that I registered the listener with SENSOR_DELAY_FASTEST ? Having one listener for every sensor or having one listener and separating the sensors with a switch?

Thank you very much in advance

Minos

解决方案

A android application should use one SensorEventListener per Sensor as every SensorEventListener creates its own FIFO buffer to process the SensorEvent. The consumption level of the current through the chip is related with the delay provided to the sensor irrespective of the SensorEventListener it is attached with.

If multiple sensors are used with one SensorEventListener than some events may get lost, due to buffer overflow, when the sensors are used at SENSOR_DELAY_FASTEST.

So,For best results and to ensure that no event is lost:

  • Use one SensorEventListener for one sensor.

    For increase in efficiency:

  • when pausing the application, un-register all the sensor registered ,if they are not required, so they do not get events from the chip even after the application is closed.

这篇关于SensorManager:一个SensorEventListener VS多个侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆