SensorManager: One SensorEventListener VS Multiple Listeners

百般思念 提交于 2019-12-10 16:35:27

问题


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


回答1:


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.


来源:https://stackoverflow.com/questions/22816089/sensormanager-one-sensoreventlistener-vs-multiple-listeners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!