如何使用Android Studio输出温度

qvtsj1bj  于 2023-02-14  发布在  Android
关注(0)|答案(1)|浏览(221)

我正在尝试开发一个简单的应用程序,它使用SensorManager读取并设置温度传感器的值来输出温度。但是每当我运行代码时,没有任何输出,我收到一条消息说SensorManager一直停止。有人能指出问题的根源吗?以下是我的代码:
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>
k3bvogb1

k3bvogb11#

看起来用以下代码替换onSensorChanged函数中的代码解决了我的问题:

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

另外,我必须移动我的虚拟传感器中的栏,这样一个数字才会实际输出。

相关问题