添加文本视图以显示温度参数处于(异常,正常)并向应用程序用户显示通知(firebase)

Add textview to show the temperature parameter is in (abnormal, normal) and show notification to app user (firebase)

left shown firebase database, right shows the data retrieve from firebase in android studio(want to implement the textView which can shown (abnormal,normal) condition when exceed 30 Celcius)

我目前正在使用 firebase 数据库在 Android Studio 中检索实时参数。我想用TextView通过显示异常(一旦超过30)和正常(低于30)条件来显示参数满足条件。任何可以显示 this.Thanks

的方式
public class RealtimeData extends AppCompatActivity {

private DatabaseReference mDatebase;


private TextView mTempView;
private TextView mHumidView;
;

//CHANGE WIFISTATE STARTS
WifiManager wifiManager;
TextView wifiStatusTextView;

//start

//end

Switch wifiSwitch;
//CHANGE WIFISTATE ENDS

@Override
protected void onCreate(Bundle savedInstanceState) {
    //test dialog out start
    buildDialog(RealtimeData.this).show();
    //test dialog out end
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realtime_data);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    //CHANGE WIFISTATE STARTS
    Toast.makeText(RealtimeData.this,"Make sure device is connected to The internet for latest data", Toast.LENGTH_LONG).show();
    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiStatusTextView = (TextView)findViewById(R.id.wifi_status);
    wifiSwitch = (Switch) findViewById(R.id.wifiswitch);

    if (wifiManager.isWifiEnabled()){
        wifiStatusTextView.setText("Wifi status: ON");
        wifiSwitch.setChecked(true);
    }else{
        wifiStatusTextView.setText("Wifi status: OFF");
        wifiSwitch.setChecked(false);
    }

    wifiSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked){
                wifiManager.setWifiEnabled(true);
                wifiStatusTextView.setText("Wifi status: ON");
                Toast.makeText(RealtimeData.this,"WIFI may take a moment to turn on", Toast.LENGTH_LONG).show();
            }else{
                wifiManager.setWifiEnabled(false);
                wifiStatusTextView.setText("Wifi status: OFF");
            }
        }
    });
    //CHANGE WIFISTATE ENDS

    mDatebase = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
    mTempView = (TextView) findViewById(R.id.tempvalue);

    mDatebase.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
        @Override
        public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
            for (com.google.firebase.database.DataSnapshot datasnap: dataSnapshot.getChildren()){
                String temp = datasnap.child("airtemperature").getValue(String.class);
                mTempView.setText("Air Temperature: " + temp + " *C");
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            mTempView.setText("Air Temperature: Error");

        }
    });

和xml文件是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#9575CD"
tools:context="com.dropbox.core.examples.android.RealtimeData">


<!--make sure android:id="@+id/include are not appear(automatically) in this 
column below (<include layout)-->
<include
    layout="@layout/app_bar" />

<TextView
    android:id="@+id/datavalue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DATA VALUE"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="36sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.193" />

<TextView
    android:id="@+id/lastupdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Last Update"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.283" />

<TextView
    android:id="@+id/watertempvalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Water Temperature:"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.672" />

<TextView
    android:id="@+id/tempvalue"
    android:layout_width="294dp"
    android:layout_height="wrap_content"
    android:text="Air Temperature:"
    android:textColor="#5D4037"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textView2"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.54" />

<TextView
    android:id="@+id/humidvalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="54dp"
    android:text="Air Humidity:"
    android:textColor="#004D40"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.667" />

<TextView
    android:id="@+id/datevalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Date: "
    android:textColor="#3E2723"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.385" />

<TextView
    android:id="@+id/timevalue"
    android:layout_width="0dp"
    android:layout_height="34dp"
    android:text="Time:"
    android:textColor="#3E2729"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.457" />

<TextView
    android:id="@+id/pHvalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="pH: "
    android:textColor="#795548"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.732" />

<TextView
    android:id="@+id/levelvalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Water level: "
    android:textColor="#424242"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.927" />

<TextView
    android:id="@+id/CO2value"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="CO2 :"
    android:textAllCaps="false"
    android:textColor="#01579B"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.792" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="128dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:text="Abnormal"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/tempvalue"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.54" />

<TextView
    android:id="@+id/wifi_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wi-Fi status:"
    android:textColor="@android:color/background_dark"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.807"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.104" />

<!-- Start of Text TVOC button
<TextView
    android:id="@+id/TVOCvalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="TVOC: "
    android:textColor="#3E2723"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.836" />

    End of Text TVOC button-->

<TextView
    android:id="@+id/TDSvalue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:text="TDS :"
    android:textColor="#212121"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.867" />

<Switch
    android:id="@+id/wifiswitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/background_dark"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.162" />

    </android.support.constraint.ConstraintLayout>

编辑的部分显示在这里

if (Float.valueOf(temp) < 30) {
        mTempView.setText("Air Temperature: " + temp + " *C" + " Normal");
    } else {
        mTempView.setText("Air Temperature: " + temp + " *C" + " Abnormal");
        public void showNotification(View view)
        {

          NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
          builder.setSmallIcon(R.drawable.impressive);
          builder.setContentTitle("Notification Alert");
          builder.setContentText(" Air Temperature abnormal!!");
          Intent intent = new Intent(this,RealtimeData.class);
          TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
          stackBuilder.addParentStack(RealtimeData.class);
          stackBuilder.addNextIntent(intent);
          PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
          builder.setContentIntent(pendingIntent);
          NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          NM.notify(0,builder.build());

        }
    }

要解决此问题,请使用以下代码:

@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
    for (com.google.firebase.database.DataSnapshot datasnap: dataSnapshot.getChildren()){
        String temp = datasnap.child("airtemperature").getValue(String.class);
        if (Float.valueOf(temp) < 30) {
            mTempView.setText("Air Temperature: " + temp + " *C" + " Normal");
        } else {
            mTempView.setText("Air Temperature: " + temp + " *C" + " Abnormal");
        }
    }
}