java - Firebase realtime Database -
i have question i'm new in android app development.
here mainactivity.java
package com.koridevbrowser.app; import android.app.alertdialog; import android.content.dialoginterface; import android.graphics.bitmap; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.keyevent; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.webkit.webchromeclient; import android.webkit.websettings; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.progressbar; import android.widget.textview; import android.widget.toast; 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 implement valueeventlistener{ private textview mtext; private webview mwebview; progressbar progressbar; private toolbar toolbar; private databasereference database = firebasedatabase.getinstance().getreference(); private databasereference mconf = database.child("version"); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.app_bar); setsupportactionbar(toolbar); mtext = (textview) findviewbyid(r.id.about); mwebview = (webview) findviewbyid(r.id.activity_main_webview); progressbar = (progressbar) findviewbyid(r.id.progressbar1); string useragent = mwebview.getsettings().getuseragentstring(); mwebview.getsettings().setuseragentstring(useragent); websettings websettings = mwebview.getsettings(); websettings.setjavascriptenabled(true); mwebview.loadurl("http://www.kori-developer.com"); mwebview.setwebchromeclient(new webchromeclient()); mwebview.setwebviewclient(new hellowebviewclient()); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_main, menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(final menuitem item) { switch (item.getitemid()) { case r.id.home: mwebview.loadurl("http://kori-developer.com/"); return true; case r.id.korifiles: mwebview.loadurl("http://kori-developer.com/forum/11-koridev-releases/"); return true; case r.id.korirules: mwebview.loadurl("http://kori-developer.com/forum/26-foren-regeln/"); return true; case r.id.koridonate: mwebview.loadurl("http://kori-developer.com/store/"); return true; case r.id.koricontact: mwebview.loadurl("http://kori-developer.com/contact/"); return true; case r.id.koriabout: toast.maketext(mainactivity.this, "version: 0.3b", toast.length_long).show(); return true; default: return super.onoptionsitemselected(item); } } @override public void ondatachange(datasnapshot datasnapshot) { if (datasnapshot.getvalue(string.class) !=null) { string value = datasnapshot.getkey(); if(value.equals("version")) { string text = datasnapshot.getvalue(string.class); mtext.settext(text); } } } @override public void oncancelled(databaseerror databaseerror) { } private class hellowebviewclient extends webviewclient{ @override public void onpagestarted(webview view, string url, bitmap favicon) { // todo auto-generated method stub super.onpagestarted(view, url, favicon); } @override public boolean shouldoverrideurlloading(webview webview, string url) { webview.loadurl(url); return true; } @override public void onpagefinished(webview view, string url) { // todo auto-generated method stub super.onpagefinished(view, url); progressbar.setvisibility(view.gone); } } @override public boolean onkeydown(int keycode, keyevent event) { //if key pressed if ((keycode == keyevent.keycode_back) && mwebview.cangoback()) { mwebview.goback(); return true; } return super.onkeydown(keycode, event); } public void onbackpressed() { alertdialog.builder alertdialogbuilder = new alertdialog.builder( mainactivity.this); // set title alertdialogbuilder.settitle("exit"); // set dialog message alertdialogbuilder .setmessage("do want exit koridev?") .setcancelable(false) .setpositivebutton("yes",new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int id) { // if button clicked, close // current activity mainactivity.this.finish(); } }) .setnegativebutton("no",new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int id) { // if button clicked, close // dialog box , nothing dialog.cancel(); } }); // create alert dialog alertdialog alertdialog = alertdialogbuilder.create(); // show alertdialog.show(); }
}
i try read version realtime database here:
case r.id.koriabout: toast.maketext(mainactivity.this, "version: 0.3b", toast.length_long).show();
but don't know how setup realtime database firebase.
a picture database :
its real time default...i believe. if change in firebase, it'll change on app. but, have, have hard coded toast.
you need this:
firebasedatabase database = firebasedatabase.getinstance(); databasereference myref = database.getreference("version"); // read database myref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { // method called once initial value , again // whenever data @ location updated. string value = datasnapshot.getvalue(string.class); log.d(tag, "value is: " + value); } @override public void oncancelled(databaseerror error) { // failed read value log.w(tag, "failed read value.", error.toexception()); } });
checkout doc here: https://firebase.google.com/docs/database/android/start/
Comments
Post a Comment