申明变量
private SensorManager sensor_manage;
private Sensor sensorLight;
sensor赋值
sensor_manage=(SensorManager)getSystemService(SENSOR_SERVICE);
sensorLight = sensor_manage.getDefaultSensor(Sensor.TYPE_PROXIMITY);//TYPE_ORIENTATION
sensor事件注册,此时调用的类要实现implements SensorEventListener
sensor_manage.registerListener(this,sensorLight, SENSOR_DELAY_NORMAL);
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.v("test","v0:"+sensorEvent.values[0]+"v1:"+sensorEvent.values[1]+"v2:"+sensorEvent.values[2]+"\r\n");
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
sensor使用结束后取消注册
sensor_manage.unregisterListener(this);
网友评论