Advertisement
Guest User

Exemplo Conexão Aplicativo

a guest
Sep 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.21 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() {
  2.  
  3.     override fun onCreate(savedInstanceState: Bundle?) {
  4.         super.onCreate(savedInstanceState)
  5.         setContentView(R.layout.activity_main)
  6.  
  7.         val database = FirebaseDatabase.getInstance()
  8.         val myRef = database.getReference("luz-acesa")
  9.  
  10.         val self = this
  11.  
  12.         val button = findViewById<TextView>(R.id.lblAviso)
  13.  
  14.         myRef.addValueEventListener(object : ValueEventListener {
  15.             override fun onDataChange(dataSnapshot: DataSnapshot) {
  16.  
  17.                 val value = dataSnapshot.getValue(Boolean::class.java)
  18.  
  19.                 if (value == true){
  20.                     val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
  21.                     val r = RingtoneManager.getRingtone(applicationContext, notification)
  22.                     r.play()
  23.                     button.setText("Luz Acesa! Cuidado!")
  24.                 } else {
  25.                     button.setText("Luz Apagada")
  26.                 }
  27.             }
  28.  
  29.             override fun onCancelled(error: DatabaseError) {
  30.                 Toast.makeText(self,  "Erro: ${error.message}", Toast.LENGTH_SHORT).show()
  31.             }
  32.         })
  33.  
  34.  
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement