tags:

views:

81

answers:

1

Hi!

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é

A: 

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!