BKSingh
BKSingh

Reputation: 527

onSensorChanged in android continuously getting triggered in emulator

I am using ACCELEROMETER sensors and have registered a listener for the same via

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAcceleratorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAcceleratorSensor , SensorManager.SENSOR_DELAY_NORMAL);

This is how my onSensorChanged looks like

@Override
public final void onSensorChanged(SensorEvent event) {
    int sensorType = event.sensor.getType();
    switch(sensorType){
        case Sensor.TYPE_ACCELEROMETER:
            float valueX = event.values[0];
            float valueY = event.values[1];
            float valueZ = event.values[2];
            Log.d(TAG, "Sensor Changed value:"+valueX+":"+valueY+":"+valueZ);
            break;

    }

However this is what I see in my logs

01-12 02:01:18.063 19691-19691/com.taxis.locationupdates2 D/LocationActivity: Sensor Changed value:2.0:2.0:2.0
01-12 02:01:18.129 19691-19691/com.taxis.locationupdates2 D/LocationActivity: Sensor Changed value:2.0:2.0:2.0

It keeps calling the onSensorChanged in infinite loop even though there is no change in the values. I havent tested it with a real device yet. Is there any settings to control the same.

Upvotes: 4

Views: 2731

Answers (2)

Alex
Alex

Reputation: 19124

That method is very poorly named.

From the android docs for onSensorChanged:

Called when there is a new sensor event. Note that "on changed" is somewhat of a misnomer, as this will also be called if we have a new reading from a sensor with the exact same sensor values (but a newer timestamp).

Upvotes: 4

BKSingh
BKSingh

Reputation: 527

The sensor documentation has the details about the same

http://developer.android.com/guide/topics/sensors/sensors_overview.html

The default data delay is suitable for monitoring typical screen orientation changes and uses a delay of 200,000 microseconds. You can specify other data delays, such as SENSOR_DELAY_GAME (20,000 microsecond delay), SENSOR_DELAY_UI (60,000 microsecond delay), or SENSOR_DELAY_FASTEST (0 microsecond delay). As of Android 3.0 (API Level 11) you can also specify the delay as an absolute value (in microseconds).

The delay that you specify is only a suggested delay. The Android system and other applications can alter this delay. As a best practice, you should specify the largest delay that you can because the system typically uses a smaller delay than the one you specify (that is, you should choose the slowest sampling rate that still meets the needs of your application). Using a larger delay imposes a lower load on the processor and therefore uses less power.

Upvotes: 0

Related Questions