Android - user-defined delay is used in registerListener(). Not working, why?

荒凉一梦 提交于 2019-12-14 02:20:11

问题


I am trying to read the values from the android phone sensors. I initialize the sensors as follows:

private void sensorInit() {
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_FASTEST);
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
            SensorManager.SENSOR_DELAY_FASTEST);
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
            SensorManager.SENSOR_DELAY_FASTEST);
}

Then I can get ~350 samples/second.

But then to save power, I hope to slow it down. So I modify SensorManager.SENSOR_DELAY_FASTEST to 1000. I wish the sensor delay to be 1000ms. But obviously it is not working, because I still get a rate of ~350 samples/second.

From Android page:

public boolean registerListener (SensorListener listener, int sensors, int rate, Handler handler)

Parameters

listener: sensor listener object

sensors: a bit masks of the sensors to register to

rate: rate of events. This is only a hint to the system. events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST. or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of the SENSOR_DELAY_* constants.

handler: The Handler the sensor events will be delivered to.

Anybody can help me?

How can I really slow the sensor sampling down to save power? Thanks in advance.


回答1:


Try SensorManager.SENSOR_DELAY_NORMAL or SensorManager.SENSOR_DELAY_UI




回答2:


To contribute a bit:

If you use any of the recommended values eg SensorManager.SENSOR_DELAY_NORMAL or SensorManager.SENSOR_DELAY_UI than these will be used. If you take a look on the implementation:

private static int getDelay(int rate) {
        int delay = -1;
        switch (rate) {
            case SENSOR_DELAY_FASTEST:
                delay = 0;
                break;
            case SENSOR_DELAY_GAME:
                delay = 20000;
                break;
            case SENSOR_DELAY_UI:
                delay = 66667;
                break;
            case SENSOR_DELAY_NORMAL:
                delay = 200000;
                break;
            default:
                delay = rate;
                break;
        }
        return delay;
    }

You can see that the delay will default to any other value except the below :

/** get sensor data as fast as possible */
    public static final int SENSOR_DELAY_FASTEST = 0;
    /** rate suitable for games */
    public static final int SENSOR_DELAY_GAME = 1;
    /** rate suitable for the user interface  */
    public static final int SENSOR_DELAY_UI = 2;
    /** rate (default) suitable for screen orientation changes */
    public static final int SENSOR_DELAY_NORMAL = 3;

So as long as your value is not 0,1,3 then you will be ok and yours should be used.




回答3:


An alternative I have used in my projects. It goes like this:

  1. Set an Alarm (android.app.AlarmManager) that would be setRepeating every x seconds.
  2. In the onReceive of the alarm registerListener with the sensor setting of your convenience.
  3. In the SensorEventListener's onSensorChanged use the event's values as you need. Then unregisterListener.

Steps 2 and 3 will repeat every x seconds.

I wish I would paste in all the code but it's quite long and intertwined with other functionality and I am sure you can find all the details online.




回答4:


Pay attention, that param samplingPeriodUs takes microseconds. So if you want your updates with 1s period. You should pass 1_000_000 to function



来源:https://stackoverflow.com/questions/16783325/android-user-defined-delay-is-used-in-registerlistener-not-working-why

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