2

I've read the Environment Sensors documentation so I'm trying to get the pressure, but unsuccessfully.

This is what I have:

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);
}
Rick
  • 3,943
  • 6
  • 33
  • 45
  • Does you phone or device have the sensor? Its been my experience that sensors vary wildly from device to device; and I have never encountered a pressure sensor in the wild. (I use the sensors to seed my random number generators, so I look at them often). – jww Mar 28 '15 at 23:52
  • I thought my galaxy ace had it.... ahaha. My code is ok, i tried on the note3. Watching it running I'm noticing that the value changes continously, is there a way to stop it? Perhaps after two decimals? – Rick Mar 29 '15 at 00:30
  • Very good (and thanks for the tip on where to find a device with that sensor). I moved to close as a "could not duplicate", but its still a good question, so my +1 stands. – jww Mar 29 '15 at 00:43

0 Answers0