Firebase can power your app’s backend, including data storage, user authentication, static hosting, and more. We provide these services so you can focus on creating extraordinary user experiences.
- Firebase Realtime Database: synchronized in realtime to every connected client.
- Firebase Authentication: built-in functionality for authenticating users with email & password, Facebook, Twitter, GitHub, Google, and anonymous auth.
-
Firebase Hosting: Deploy our web app in seconds with production-grade static asset hosting. All of our content is delivered over SSL from there global CDN.
Get Started with Firebase
- Create an account on firebase or login to FireBase and get the standard FireBase data URL where our data is stored.
- Create a project in android studio and add dependency:
|
1 |
compile 'com.firebase:firebase-client-android:2.3.1' |
NOTE: If you are getting a build error complaining about duplicate files you can choose to exclude those files by adding the packagingOptions directive to your build.gradle file:
|
1 2 3 4 5 6 7 8 |
android { ... packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt' exclude 'META-INF/NOTICE' } } |
-
Add Android Permissions
|
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
- Setup Firebase on Android
|
1 2 3 4 5 6 7 8 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Firebase.setAndroidContext(this); Firebase myFirebaseRef = new Firebase("https://glaring-inferno-8411.firebaseio.com/"); ... } |
-
Reading Data
Reading data from your Firebase database is accomplished by attaching an event listener and handling the resulting events. Assuming we already wrote to myFirebaseRef above, we can retrieve the message value by using the addValueEventListener method:
|
1 2 3 4 5 6 7 |
myFirebaseRef.child("message").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { System.out.println(snapshot.getValue()); //prints "Do you have data? You'll love Firebase." } @Override public void onCancelled(FirebaseError error) { } }); |
-
Writing Data
Once we have a reference to your data, we can write any Boolean, Long, Double, Map<String, Object> or List object to it using setValue():
|
1 |
myFirebaseRef.child("message").setValue("Do you have data? You'll love Firebase."); |
Stay Updated!!!
