在firebase

时间:2017-09-30 06:46:29

标签: java android firebase firebase-authentication

我打算创建一个聊天应用程序。例如,我有两个EditText,它接收电子邮件和密码。我想在Google Firebase上使用电子邮件和密码创建用户身份验证。我的代码不起作用。我找不到我的错误。请给我一个解决方案。

activity_main

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.atik_faysal.test.MainActivity">

    <EditText
        android:id="@+id/e_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="52dp"
        android:layout_marginStart="52dp"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/e_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:ems="10"
        android:inputType="textPassword"
        android:layout_below="@+id/e_email"
        android:layout_alignLeft="@+id/e_email"
        android:layout_alignStart="@+id/e_email" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="signIn"
        android:layout_marginTop="16dp"
        android:text="Log in"
        android:layout_below="@+id/e_password"
        android:layout_alignLeft="@+id/e_password"
        android:layout_alignStart="@+id/e_password" />

    <Button
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="registerUser"
        android:text="registerUser"
        android:layout_alignBaseline="@+id/login"
        android:layout_alignBottom="@+id/login"
        android:layout_toRightOf="@+id/login"
        android:layout_toEndOf="@+id/login"
        android:layout_marginLeft="27dp"
        android:layout_marginStart="27dp" />
</RelativeLayout>

MainActivity.java

    package com.example.atik_faysal.test;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.firebase.client.Firebase;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;


public class MainActivity extends Activity {

    private EditText editTextEmail;
    private EditText editTextPassword;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firebaseAuth = FirebaseAuth.getInstance();
        editTextEmail = (EditText) findViewById(R.id.e_email);
        editTextPassword = (EditText) findViewById(R.id.e_password);
    }

    public void registerUser(View view){
        String email = editTextEmail.getText().toString().trim();
        String password  = editTextPassword.getText().toString().trim();
        if(TextUtils.isEmpty(email)){
            Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
            return;
        }
        if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
            return;
        }
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(MainActivity.this,"Registration Complete",Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
                        }
                    }
                });

    }

}

应用程序级依赖性

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
    defaultConfig {
        applicationId "com.example.atik_faysal.test"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/mail.jar')
    compile 'com.google.firebase:firebase-database:11.4.0'
    compile 'com.google.firebase:firebase-auth:11.4.0'
    compile 'com.google.firebase:firebase-crash:11.4.0'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.firebase:firebase-client-android:2.3.1'
    compile 'com.cuboid:cuboidcirclebutton:1.0.3'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
    testCompile 'junit:junit:4.12'
    debugCompile 'com.android.support.constraint:constraint-layout:1.0.2'
}
apply plugin: 'com.google.gms.google-services'

1 个答案:

答案 0 :(得分:0)

如何使您的应用接受特定链接,例如

<data android:pathPrefix="www.example.com" android:scheme="http"/>

然后你发送一个链接,例如http://www.example.com/abcd,电子邮件和用户应该使用应用程序打开它,最后应用程序将检查“打开的链接”链接是否与“已发送链接”相同。