gpt4 book ai didi

java - Firebase 实时数据库

转载 作者:行者123 更新时间:2023-11-30 00:21:27 26 4
gpt4 key购买 nike

我有一个问题 我是 Android 应用程序开发的新手。

这是我的 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 back key is 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 you really want to exit KoriDev?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}

我会在这里尝试从实时数据库中读取版本:

case R.id.KoriAbout:
Toast.makeText(MainActivity.this, "Version: 0.3b", Toast.LENGTH_LONG).show();

但我不知道如何使用 Firebase 的实时数据库进行设置。

我数据库中的一张图片:

enter image description here

最佳答案

默认是实时的……我相信。如果您在 firebase 中更改它,它会在您的应用程序中更改它。但是,对于你所拥有的,你已经将它硬编码到你的 toast 中。

你需要做这样的事情:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("version");

// Read from the database
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);
Log.d(TAG, "Value is: " + value);
}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});

在这里查看他们的文档: https://firebase.google.com/docs/database/android/start/

关于java - Firebase 实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46206454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com