气压 - 没有得到价值

时间:2015-03-28 23:29:53

标签: android pressure

我已阅读Environment Sensors文档,因此我尝试获取pressure,但未成功。

这就是我所拥有的:

public class Pressure extends Activity implements SensorEventListener{

TextView textView;
private SensorManager sensorManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_pressure);

    textView = (TextView) findViewById(R.id.txt_pressure);

    sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);

}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
        float[] values = event.values;
        textView.setText("" + values[0]);
    }

}

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

}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

0 个答案:

没有答案