IOT

  • a0_287024
    了解作者
  • 133.6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-05 23:50
    上传日期
IOT
IOT-master.zip
内容介绍
package com.example.smarthome; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.ToggleButton; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; public class MainActivity extends AppCompatActivity { DatabaseReference reff; int D2 = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ToggleButton t=(ToggleButton) findViewById(R.id.toggleButton); Button b=(Button) findViewById(R.id.button); reff= FirebaseDatabase.getInstance().getReference(); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { reff.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { if(dataSnapshot.child("D4").getValue().equals("1")){ Toast.makeText(MainActivity.this,"Your stock is full!!",Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(MainActivity.this,"Your stock is empty!!!!!!!!",Toast.LENGTH_SHORT).show(); } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); } }); t.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ reff.child("D2").setValue(1); } else{ reff.child("D2").setValue(0); } } }); } }
评论