用气压和高度显示温度值

时间:2018-01-04 20:02:15

标签: java android android-sensors

我在文本视图中显示我的温度传感器值,并在另外两个文本视图中显示来自气压传感器的气压和高度 问题是第一个文本视图显示气压而不是显示温度,其他文本视图显示正确的气压和高度值,我命名文本视图ID正确,但当我运行应用程序时,dosnt显示温度在第一个文本视图在相同的活动,如果我在两个活动中分开它,它工作得很好,但我想在同一活动中删除它们,这是我的代码

  public class TemperatureActivity extends Activity
    implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private boolean isSensorPresent;
private TextView mTemperatureValue;
private TextView mPressureValue;
private TextView mAltitudeValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager =
            (SensorManager)this.getSystemService
                    (Context.SENSOR_SERVICE);
    mTemperatureValue =
            (TextView)findViewById(R.id.temperaturetext);
    if(mSensorManager.getDefaultSensor
            (Sensor.TYPE_AMBIENT_TEMPERATURE) != null) {
        mSensor = mSensorManager.getDefaultSensor
                (Sensor.TYPE_AMBIENT_TEMPERATURE);
        isSensorPresent = true;
    } else {
        mTemperatureValue.setText("Ambient Temperature Sensor is not available!");
        isSensorPresent = false;
    }


    mPressureValue =
            (TextView)findViewById(R.id.pressuretext);
    mAltitudeValue =
            (TextView)findViewById(R.id.altitudetext);
    mSensorManager = (SensorManager)
            this.getSystemService(Context.SENSOR_SERVICE);
    if(mSensorManager.getDefaultSensor
            (Sensor.TYPE_PRESSURE) != null) {
        mSensor = mSensorManager.getDefaultSensor
                (Sensor.TYPE_PRESSURE);
        isSensorPresent = true;
    } else {
        isSensorPresent = false;
        mPressureValue.setText("Pressure Sensor is not available!");
        mAltitudeValue.setText("Cannot calculate altitude, as pressure 
     Sensor is not available!");
    }





}
@Override
protected void onResume() {
    super.onResume();
    if(isSensorPresent) {
        mSensorManager.registerListener(this, mSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
}
@Override
protected void onPause() {
    super.onPause();
    if(isSensorPresent) {
        mSensorManager.unregisterListener(this);
    }
}
@Override
public void onSensorChanged(SensorEvent event) {
    mTemperatureValue.setText("Temperature in degree Celsius is " + 
   event.values[0]);

   float pressure = event.values[0];
    mPressureValue.setText("Pressure in mbar is " +
            pressure);
    float altitude = SensorManager.getAltitude
            (SensorManager.PRESSURE_STANDARD_ATMOSPHERE,
                    pressure);
    mAltitudeValue.setText("Current altitude is " +
            altitude);

}

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

  }



}

1 个答案:

答案 0 :(得分:0)

@Override
public void onSensorChanged(SensorEvent event) {
    mTemperatureValue.setText("Temperature in degree Celsius is " + 
   event.values[0]);

   float pressure = event.values[0];
    mPressureValue.setText("Pressure in mbar is " +
            pressure);
    float altitude = SensorManager.getAltitude
            (SensorManager.PRESSURE_STANDARD_ATMOSPHERE,
                    pressure);
    mAltitudeValue.setText("Current altitude is " +
            altitude);

}

彻底破碎了。每个传感器事件,您都将相同的值写入多个字段。每个传感器可以获得一个事件。您应该仅在发生温度事件时更新温度,仅在遇到压力事件时更新压力等