본문 바로가기

Android/기능

[Android] 파이어베이스 리얼타임 데이터베이스(Realtime Database) 저장하기

데이터베이스 생성하기

 

데이터베이스 만들기
가까운 미국으로 설정함

 

데이터 베이스 연결하기

 

https://firebase.google.com/docs/database/android/start?hl=ko 

 

Firebase에 앱 연결  |  Firebase Realtime Database

Join us in person and online for Firebase Summit on October 18, 2022. Learn how Firebase can help you accelerate app development, release your app with confidence, and scale with ease. Register now 의견 보내기 Firebase에 앱 연결 컬렉션을 사

firebase.google.com

implementation 'com.google.firebase:firebase-database-ktx'

- 위에 코드를 buid.gradle(Module)에 넣고 Sync Now하기

 

activity_main.xml에 저장하기 버튼을 만듬

 

<Button
    android:id="@+id/saveBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="저장하기"/>

 

 

MainActivity.kt에 저장하기 버튼을 누르면 파이어베이스에 저장하도록 함

 

// 저장하기 기능
val saveBtn = findViewById<Button>(R.id.saveBtn)
saveBtn.setOnClickListener {

    // Write a message to the database
    val database = Firebase.database
    val myRef = database.getReference("message")

    // myRef.setValue("저장하기")
    myRef.push().setValue("저장하기") // push()를 입력하면 계속 저장이 가능해짐

}

 

파이어베이스에 저장됨