Java 什么是 firebase 以及如何在 Android 中使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33720614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:57:44  来源:igfitidea点击:

What is firebase and how to use it in Android?

javaandroidfirebasefirebase-hostingfirebase-realtime-database

提问by Nirup Iyer

I want to create an android app, where I, as an author, upload my college notes and anyone can download it. I read that firebase can help me with this. Can anyone please explain what is firebase and how to use it with my reference? Thank you!

我想创建一个 android 应用程序,作为作者,我可以在其中上传我的大学笔记,任何人都可以下载它。我读到 firebase 可以帮助我解决这个问题。任何人都可以解释什么是 firebase 以及如何在我的参考中使用它?谢谢!

采纳答案by David East

Update: Since Google I/O 2016 there have been some major updates to Firebase. Below is information related to the legacy service.

更新:自 2016 年 Google I/O 大会以来,Firebase 发生了一些重大更新。以下是与旧版服务相关的信息。

Firebase team member here.

Firebase 团队成员在这里。

tl;dr - Read this Quickstart, watch this video. Use FirebaseUI.

tl;dr - 阅读此快速入门,观看此视频。使用FirebaseUI

Firebase is a platform for mobile and web apps.

Firebase 是一个适用于移动和网络应用程序的平台。

There's three main services to Firebase:

Firebase 提供三个主要服务:

  • Realtime database
  • Authentication
  • Static Hosting
  • 实时数据库
  • 验证
  • 静态托管

Setup

设置

For writing an Android app you need to download the Android SDK. If you have Android Studio 1.4 you can setup Firebase by going to File > Project Structure > Cloud. Then click the Firebase checkbox.

要编写 Android 应用程序,您需要下载 Android SDK。如果您有 Android Studio 1.4,您可以通过转至File > Project Structure > Cloud. 然后单击 Firebase 复选框。

Saving and Retrieving data

保存和检索数据

Every Firebase app has a name, and that is used to in a URL to access your database. Data is stored in Firebase in JSON. Each piece has a URL mapped to its location. To get or save data to that location you create a Firebase reference.

每个 Firebase 应用程序都有一个名称,用于在 URL 中访问您的数据库。数据以 JSON 格式存储在 Firebase 中。每个部分都有一个映射到其位置的 URL。要获取或保存数据到该位置,您需要创建一个 Firebase 引用。

// Create a reference to the Firebase database
Firebase ref = new Firebase("https:<MY-FIREBASE-APP>.firebaseio.com/data");
// Save Data
ref.setValue("Hello"); 
// Sync data
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

FirebaseUI

Firebase用户界面

The Firebase SDK is good at saving and retrieving data, but it is agnostic of Android SDK components like ListAdapters. For that you can use the FirebaseUI library.

Firebase SDK 擅长保存和检索数据,但它与ListAdapters等 Android SDK 组件无关。为此,您可以使用FirebaseUI 库

FirebaseUI allows you to quickly connect common UI elements to the Firebase database for data storage. Below is an example of using FirebaseUI with a FirebaseListAdapter.

FirebaseUI 允许您将常用 UI 元素快速连接到 Firebase 数据库以进行数据存储。以下是将 FirebaseUI 与FirebaseListAdapter.

mAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, ref) {
    @Override
    protected void populateView(View view, ChatMessage chatMessage) {
        ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
        ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage());

    }
};
messagesView.setListAdapter(mAdapter);

That's just the gist of everything. The documentation of Firebaseis pretty comprehensive (and human readable if I do so myself).

这就是一切的要点。Firebase文档非常全面(如果我自己这样做的话,也是人类可读的)。

回答by Harry

Firebase is a NoSQL type database that makes use of sockets, which allows the client to receive information live - without having to make GET requests to the server.

Firebase 是一种使用套接字的 NoSQL 类型数据库,它允许客户端实时接收信息 - 无需向服务器发出 GET 请求。

This requires that when you set things up you 'subscribe' the client to the database/collection.

这要求您在设置时将客户端“订阅”到数据库/集合。

In terms of how you could use it in an application, that depends on the technologies you wish to use in your stack. The firebase website has documentation advising you on how to do this.

就如何在应用程序中使用它而言,这取决于您希望在堆栈中使用的技术。firebase 网站上有文档建议您如何执行此操作。

Also, if you are asking solely on the basis of wishing to have the functionality you have described; the Meteor framework comes with a sockets based, NoSQL, backend database which is very easy to implement; there is a tutorial here; https://www.meteor.com/tutorials/blaze/creating-an-app

此外,如果您仅基于希望拥有您所描述的功能而提出要求;Meteor 框架带有一个基于套接字的 NoSQL 后端数据库,非常容易实现;这里有教程;https://www.meteor.com/tutorials/blaze/creating-an-app

回答by fuddin

Its been a while since you asked the question and Firebase has added new documentation. Here is what it says,

您问这个问题已经有一段时间了,Firebase 添加了新文档。这是它所说的,

Prerequisites

先决条件

  • Android's version should be 2.3 or newer with Google Play services 9.2.1 or newer
  • Android Studio 1.5 or higher.
  • An Android Studio project and its package name (The package name can be found from ApplicationManifest.xml).
  • Android 版本应为 2.3 或更高版本,Google Play 服务为 9.2.1 或更高版本
  • Android Studio 1.5 或更高版本。
  • 一个 Android Studio 项目及其包名(包名可以从 ApplicationManifest.xml 中找到)。

Add Firebase to your application

将 Firebase 添加到您的应用程序

  • Create an accounton Firebase.
  • Create a project in Firebase console.
  • Click Firebase for Android app and follow the instructions.
  • When prompted, enter app's package name.
  • Download google-services.json file.
  • Place google-services.json file in your app's module folder. Typically /app.
  • 在 Firebase 上创建一个帐户
  • Firebase 控制台中创建一个项目。
  • 单击适用于 Android 应用的 Firebase 并按照说明进行操作。
  • 出现提示时,输入应用程序的包名称。
  • 下载 google-services.json 文件。
  • 将 google-services.json 文件放在您应用的模块文件夹中。通常/应用程序。

Add the SDK

添加开发工具包

If you would like to integrate Firebase libraries into your projects, you need to perform few basic tasks to prepare your Android SDK project.

如果您想将 Firebase 库集成到您的项目中,您需要执行一些基本任务来准备您的 Android SDK 项目。

  • First, add rules to your root-level build.gradle file, to include the google-services plugin:

    buildscript { // ... dependencies { // ... classpath 'com.google.gms:google-services:3.0.0' } }

  • 首先,将规则添加到您的根级 build.gradle 文件中,以包含 google-services 插件:

    buildscript { // ... dependencies { // ... classpath 'com.google.gms:google-services:3.0.0' } }

Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

然后,在您的模块 Gradle 文件(通常是 app/build.gradle)中,在文件底部添加 apply plugin 行以启用 Gradle 插件:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:9.2.1'
}
// ADD THIS AT THE BOTTOM

apply plugin: 'com.google.gms.google-services'

You should also add the dependencies for the Firebase SDKs you wish to use - we recommend starting with the firebase-core, which include Firebase Analytics, but see below for the full list.

您还应该为要使用的 Firebase SDK 添加依赖项 - 我们建议从 firebase-core 开始,其中包括 Firebase Analytics,但请参阅下面的完整列表。