public class MainActivity extends Activity implements SensorEventListener {
final String tag = "myLogs";
SensorManager sm = null;
Sensor lightSensor;
float lightQuantity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
if(lightSensor == null)
Log.d(tag, "no sensor:(");
else
Log.d(tag, "GOT IT!");
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener((SensorEventListener)this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener((SensorEventListener)this);
super.onStop();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor.getType() + ", accuracy: " + accuracy);
}
public void onSensorChanged(SensorEvent event) {
lightQuantity = event.values[0];
Log.d(tag,"onSensorChanged: " + event.sensor.getType() + ", result: " + lightQuantity);
}
}
我无法使用此代码获取光传感器统计信息。“无传感器:(”消息始终。getSensorList()方法也不显示光传感器。我的设备:Xperia PRO(mk16i)。SE光传感器测试效果很好