我是在 android 中使用传感器的新手,现在有点困惑。只有当光线发生明显变化时,我才需要执行一些操作,例如在黑暗的房间里打开灯。到目前为止,我有一个非常简单的默认实现。如何告诉系统我只想对显着的光线变化做出反应?
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
private RelativeLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
mLayout = (RelativeLayout) findViewById(R.id.mLayout);
mLayout.setKeepScreenOn(true);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mLight,
SensorManager.SENSOR_DELAY_FASTEST);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_LIGHT) {
// TODO
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
// TODO
}
}
}