繁体   English   中英

修复 Android 中的环境温度

[英]Fix the ambient Temperature in Android

我成功地制作了一个应用程序来测量 Android 工作室中的环境温度。 但下一步不想工作。 重点是:-当我启动应用程序时,温度应显示在 1 textView 中(到目前为止有效)。 然后它应该保持这个温度,但它会改变。 我只想检查一次温度。 - 当我点击提交时,它应该与第二个 textView 做同样的事情。

我用时间成功地做到了这一点,但用温度,它不会起作用。 我的第二个问题是,我做了很多研究,看起来温度总是以°c 显示。 我可以改变它,以便想要它在°F的人可以这样吗?

无需更改值的温度代码以及用于在 textView2 上获取温度的按钮:

主要活动:

     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>

我希望有人可以帮助我走向更好的方向。

谢谢你的帮助。

在第一个代码中,您有一个关于传感器的部分已更改,因此每次都会运行导致您的问题。 在您创建所有 API 以获取温度一次并将其作为字符串放入文本视图。 对于°C 到°F,您需要进行转换。 我会手动执行此操作:

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

其中 X 是输入温度,结果是 output(°F)。

这是按钮温度更改功能的示例,不确定您如何读取温度,因此 Sensor.gettemp 行可能需要更改。 从按钮按下运行它,它将更新您的文本视图

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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