javaandroidxmlsensormanager

How to output temperature using Android Studio


I am trying to develop a simple app that outputs the temperature using a SensorManager to read and set the value from the temperature sensor. But whenever I run the code, nothing outputs, and I get a message saying the SensorManager keeps stopping. Can someone please point out what the root of the problem is? Here is my code:

MainActivity.java

    package com.zybooks.sensormanager;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor thermometer;
    private TextView tempValues;

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

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        thermometer = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        tempValues = findViewById(R.id.temperatureValues);

    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {
            tempValues.setText((int) event.values[0]);
    }

    @Override
    protected void onResume() {
        // Register a listener for the sensor.
        super.onResume();
        sensorManager.registerListener(this, thermometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        // Be sure to unregister the sensor when the activity pauses.
        super.onPause();
        sensorManager.unregisterListener(this, thermometer);
    }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="419dp"
        android:layout_height="91dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/barTitle"
        android:layout_width="179dp"
        android:layout_height="36dp"
        android:layout_marginBottom="23dp"
        android:text="Thermometer"
        android:textColor="#8BC34A"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="@+id/toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/toolbar" />

    <TextView
        android:id="@+id/temperatureValues"
        android:layout_width="191dp"
        android:layout_height="77dp"
        android:text="Temperature"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintVertical_bias="0.44" />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • It looks like replacing the code in the onSensorChanged function with this code solved my problem:

    float ambient_temperature = event.values[0];
    tempValues.setText("Temperature:\n " + String.valueOf(ambient_temperature));
    

    Plus, I had to move the bar in my virtual sensor so a number would actually output.