简体   繁体   中英

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: - when I start the app the temperature should be shown in 1 textView (works so far). Then it should stay with this temperature but it changes. I only want to check the temperature once. - when I click on submit it shall do the same with the second textView.

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:

     package com.example.sensortemp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

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;
        }
    }
}

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="100dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="241dp"
        android:layout_marginBottom="337dp"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.45" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="248dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="117dp"
        android:layout_marginBottom="337dp"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.45" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        tools:layout_editor_absoluteX="172dp"
        tools:layout_editor_absoluteY="453dp" />

</RelativeLayout>

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

Thanks for your help.

In the first code you have a section which is on sensor changed so this will run each time causing your issue. In you oncreate all the API to get the temperature once and chuck it to the text view as a string. For °C to °F you will need to convert. I would just do this manually:

(X°C × 9/5) + 32 = result_°F

Where X is the input temperature and result is the output in °F.

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;
        }
    }

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