二手LG Nexus 4,Android 4.3
返回值有时是假的,我不明白为什么.文档说:"如果传感器得到支持并成功启用,则为true."
奇怪的是,我见过的没有例子是测试SensorManager.registerListener的返回值.
奇怪的是,如果我忽略了返回错误的值,那么一切都按预期工作!
文档:http: //developer.android.com/reference/android/hardware/SensorManager.html
android.hardware.SensorManager,android.hardware.SystemSensorManager
我的代码:
SensorManager sensorMgr = (SensorManager)_context.getSystemService(Context.SENSOR_SERVICE);
if (sensorMgr == null) {
Log.w(TAG, "200410::Sensors not supported");
return false;
}
Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);
result = sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
if( result == false) {
// Normally something should be done here
// since the false return value indicated that registration failed.
// But I found it's better doing nothing since the registration seams to be ok
}
return result;
Run Code Online (Sandbox Code Playgroud)
在Android 4.3之前没有发生奇怪的错误返回值 …