●程式說明

(一)Firebase Realtime Database

=>使用雲端資料庫交換資料

=>先連結google帳號和firebase

 
1.開新專案=>tools=>firebase
firebase_realtime_database
 
2.右邊開啟視窗,選Realtime Database
firebase_realtime_database
 
3.點Save and retrieve data,第一步先connect to Firebase,完成後會變成綠色的
firebase_realtime_database
 
用project模式看,會發現app下面自動產生了一個google-service.json檔,裡面存了金鑰
firebase_realtime_database
 
4.第二步點add the realtime database to your app,會自動幫你做好設定
firebase_realtime_database
 
選擇是否要在firebase新建專案 或是使用舊專案
firebase_realtime_database
 
5.第三步,回firebase看到新建的專案,點進去=>database=>開始使用=>規則,會看到如下內容
=>"auth != null"代表驗證是空的就不行讀取,沒驗證的話不能讀取
=>把內容改成read:true,write:true
=>發佈
firebase_realtime_database
 
=>規則公開,任何人都可以讀取,開放資料庫讓app讀取
firebase_realtime_database
 
(二)回到android程式
public class MainActivity extends AppCompatActivity {
TextView tv;
EditText et;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef;
 
@Override.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
et=(EditText) findViewById(R.id.et);
}
 
public void write(View view) {
myRef = database.getReference("message");
myRef.setValue(et.getText().toString());
}
 
public void read(View view) {myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
tv.setText(value);
 
}
 
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
//Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
}
**說明:
1.FirebaseDatabase database = FirebaseDatabase.getInstance();=>連結資料庫
2. myRef = database.getReference("message");=>連結資料,資料名稱為message
3.myRef.setValue=>把message資料寫入
4.寫入完成可以去firebase看資料,可以看到message中有value
5.讀取時,增加一個值的監聽器myRef.addValueEventListener ,隨時監聽資料有無改變,當值一改變就觸發下面的程式,因為是放在click裡面,所以只要點一次按鈕,就會一直持續監聽,如果要取消監聽,可以用removelistener,但是remove的話要把監聽做成一個物件,才能使用
 
=>資料可以有很多reference,大量資料可以存成jason來用
 
 
(三)全域變數如果要同時給很多個activity使用,有兩種做法
1.直接在mainactivity宣告:public static String 變數
2.宣告一個類別來繼承application,在這個類別中宣告public 變數,不需要static
firebase_realtime_database
public class MyApplication extends Application {
    public String str;
}
 
並且在AndroidManifest.xml中設定application去抓myapplication,這邊的設定是告訴android要呼叫哪一個class來當最底層的application,所以我們要把最底層的系統application改成我們的application
<application
    android:name=".MyApplication"
    ...
</application>
 
在要使用變數的activity中加入下列程式,因為在啟動程式時,會先啟動一個最底層的application物件,然後mainactivity會放在上面,而getApplication是AppCompatActivity中的方法,可以取得這個activity最底層的application物件並回傳,但因為我們要使用我們自己寫的MyApplication,所以要轉型為我們的類別,這樣就可以直接透過.str使用str變數
MyApplication app = (MyApplication) getApplication();
app.str = ...(省略)

 

 

 

●程式參考(GitHub):透過Android Studio內建機制使用FireBase Realtime Database

arrow
arrow
    創作者介紹
    創作者 muchone 的頭像
    muchone

    簡單。生活。享受

    muchone 發表在 痞客邦 留言(0) 人氣()