javaandroidandroid-sensorstemperature

Fix the ambient Temperature in Android


I successfully made an app to measure the ambient temperature in Android studio. But the next step doesn't want to work. here's the point:

I did this successfully with the time but with the temperature, it won't work. And my second question is, I did a lot of research and it looks like the temperature will always be shown in °c. Can I change that so the people who want it in °F can have it this way?

Code for the temperature without change the value and with the button for getting the temperature on textView2:

MainActivity:

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private TextView textView, textView2;
    private Button button;
    private SensorManager sensorManager;
    private Sensor tempSensor;
    private Boolean isTemperatureSensorAvailable;

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

        textView = findViewById(R.id.textView);
        textView2 = findViewById(R.id.textView2);
        button = findViewById(R.id.button);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        if(sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) !=null) {
            tempSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
            isTemperatureSensorAvailable = true;
        }else{
            textView.setText("Temperature Sensor is not available");
            isTemperatureSensorAvailable = false;
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
    }

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

    }

    @Override
    protected void onResume(){
        super.onResume();
        if(isTemperatureSensorAvailable){
            sensorManager.registerListener(this,tempSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }


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

    private void update_text_unit()//Run from button
    {
        boolean toogle = false;
        float temperature = 0;
        float C_temp = 0;
        float F_temp =0;
        //Get sensor data
        Sensor temp = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        F_temp = (C_temp * (9/5) +32);//Convert to °F
        if(toogle = true) {
            textView2.setText("Temp is:" + C_temp + "°C");
            toogle = false;
        }
        else {
            textView2.setText("Temp is:" + F_temp + "°F");
            toogle = true;
        }
    }
}

I hope someone can help me going into a better direction.

Thanks for your help.


Solution

  • Here is an example of the button temp change feature not exactly sure how you read the temp so the line of Sensor.gettemp will probably need to change. Run this from a button push, it will update your text view

    private void update_text_unit()//Run from button
        {
            boolean toogle = false;
            float temperature = 0;
            float C_temp = 0;
            float F_temp =0;
            //Get sensor data
            C_temp = Sensor.gettemp();
            F_temp = (C_temp * (9/5) +32);//Convert to °F
            if(toogle = true) {
                yourtextview.setText("Temp is:" + C_temp + "°C");
                toogle = false;
            }
            else {
                yourtextview.setText("Temp is:" + F_temp + "°F");
                toogle = true;
            }
        }