Sibbs Gambling Sibbs Gambling - 5 months ago 50
Android Question

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

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.

Answer

Try SensorManager.SENSOR_DELAY_NORMAL or SensorManager.SENSOR_DELAY_UI