获取手机气压传感器数据

定义传感器管理器,气压传感器

private SensorManager mSensorManager;
private Sensor mBarometerSensor;

 

获取

// sensor manager
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  
//BarometerSensor
 mBarometerSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);

监听器

private SensorEventListener mBarometerSensorEventListener=new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            updateBarometer(event.values);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

注册监听器

if(mBarometerSensor!=null){
            mSensorManager.registerListener(mBarometerSensorEventListener, mBarometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

取消监听器

if(mBarometerSensor!=null){
            mSensorManager.unregisterListener(mBarometerSensorEventListener);            
 }

转载于:https://www.cnblogs.com/feike/archive/2013/01/23/2872359.html

你可能感兴趣的:(获取手机气压传感器数据)