Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 579
BKSingh Avatar asked Aug 31 '25 01:08

BKSingh


1 Answers

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).

like image 127
Alex Avatar answered Sep 02 '25 17:09

Alex