简体   繁体   中英

How to get ambient air temperature using SensorManager?

I am attempting to get ambient air temperature using SensorManager in Android. In the code below, I a button click event and, for each button click, I would like to get the ambient air temperature in a textview. The code below returns the Sensor information only. What lines of code I should include to get the ambient air temperature?

public class MainActivity extends AppCompatActivity implements SensorEventListener {

private SensorManager sensorManager;
private float temperature;

//TextView txtT;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //thermometer = (Thermometer) findViewById(R.id.thermometer);
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    final TextView txtT = (TextView) findViewById(R.id.textTemp);

    Button getButton = (Button) findViewById(R.id.button_get);

    getButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
            txtT.setText(sensor.toString());

        }
    });
}


@Override
public void onSensorChanged(SensorEvent event) {

}

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

}
}

The sensor ambient temperature has only been present in the Galaxy S4, and Note 3. I am not aware of any other smartphone that has been released with this sensor. It is very likely that you are not going to get any values from this sensor unless you have some of the phones I listed. Our devices have Battery, and CPU thermometers, but their data is not meant to be used as the data from an ambient thermometer.

You receive sensor event values in the onsensorchanged function. You were missing to register a listener for the sensor, and to retrieve the data from the onsensorchanged function.

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private float temperature;

//TextView txtT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //thermometer = (Thermometer) findViewById(R.id.thermometer);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    //    final TextView txtT = (TextView) findViewById(R.id.textTemp);

//        Button getButton = (Button) findViewById(R.id.button_get);

  //      getButton.setOnClickListener(new View.OnClickListener() {
    //        @Override
      //      public void onClick(View v) {



        sensorManager.registerListener(this, sensorManager
                        .getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_FASTEST);

        //      txtT.setText(sensor.toString());

          //  }
      //  });
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        temperature = event.values[0];
        System.out.println("Temperature " + temperature);

    }

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

    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM