aglotero
aglotero

Reputation: 31

Android SENSOR_PROXIMITY/SENSOR_LIGHT on Motorola Droid (Milestone)

I'm trying to read the status of the Proximity sensor (also I tryed to read the Light sensor...) using the following code:

@Override
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    txtStatus = (TextView)findViewById(R.id.txtStatus);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorManager.registerListener(this, SensorManager.SENSOR_PROXIMITY);
}

@Override
protected void onStart() {
    super.onStart();
    sensorManager.registerListener(this, SensorManager.SENSOR_LIGHT);
}

@Override
public void onResume(){
    super.onResume();
    sensorManager.registerListener(this, SensorManager.SENSOR_LIGHT);
}

@Override
protected void onPause() {
  super.onPause();
  sensorManager.unregisterListener(this, SensorManager.SENSOR_LIGHT);
}

The txtStatus change the default text when there is a changing in the sensor (when I try to read the Accelerometer, it works...), but when I block the light sensor nothing happens.

When I'm in a call, the sensor works (when I put my hand blocking the sensor the screen turn off).

I'm missing something here?

Regards, André

Upvotes: 1

Views: 1072

Answers (2)

Nevin Chen
Nevin Chen

Reputation: 1815

Is this all of your code? I guess you have to implement SensorEventListener with below code

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
        Log.i(TAG, "Light Change :" + event.values[0] );
    }
    if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
        Log.i(TAG, "PROXIMITY Change :" + event.values[0] );
    }
}

Besides, why do you register your sensors so many ti

Upvotes: 0

aglotero
aglotero

Reputation: 31

Oh I'm using the wrong parameter on registerListener...

instead of

sensorManager.registerListener(this, SensorManager.SENSOR_LIGHT);

use:

sensorManager.registerListener(sensorEventListener, sensorManager
                .getDefaultSensor(Sensor.TYPE_PROXIMITY),
                SensorManager.SENSOR_DELAY_FASTEST);

now works!

Upvotes: 2

Related Questions