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_SER开发者_如何转开发VICE);
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é
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!
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
精彩评论