package com.example.android.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
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;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends Activity {
private EditText name, password;
private TextView signUp;
private Button login;
String Name, Password,nm,ps;
String NAME=null, PASSWORD=null, EMAIL=null;
//to get an instance of firebase authentication
//firebaseAuth is the object of the main class FirebaseAuth (to import libraries od the authentication part of firebase)
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.ID);
password = (EditText) findViewById(R.id.password);
signUp = (TextView) findViewById(R.id.tvRegister);
login = (Button) findViewById(R.id.button);
//instance of firebase authentication
firebaseAuth = FirebaseAuth.getInstance();
//object of main class
//to check is an user is already logged in, in our app or not
//it checks with the database if a user has already logged in with our app or not
FirebaseUser user = firebaseAuth.getCurrentUser();
/*if(user !=null) //it checks if a user has already logged in
{
finish(); //finish the current activity
Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
startActivity(intent);
}*/
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
validate(name.getText().toString(),password.getText().toString());
}
});
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(i);
}
});
ImageButton holiday =(ImageButton) findViewById(R.id.holiday);
holiday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Holiday h = new Holiday();
h.execute();
}
});
}
private void validate(String userName, String userPassword) {
firebaseAuth.signInWithEmailAndPassword(userName,userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Toast.makeText(MainActivity.this,"Login Successful",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
startActivity(intent);
}
else
Toast.makeText(MainActivity.this,"Login Failed",Toast.LENGTH_SHORT).show();
}
});
}
}
我正在尝试创建登录页面,如果用户尚未注册,他/她可以注册。注册成功完成,数据存储在firebase中,但在我尝试登录时,它表示登录失败。它无法从数据库中检索数据。这是我的主要活动,即登录页面。
package com.example.android.login;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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 RegistrationActivity extends AppCompatActivity {
private EditText userName, password, userEmail;
private Button regButton;
private TextView userLogin;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(validate())
{
//Update Data to database
String user_email = userEmail.getText().toString().trim();
String pass = password.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Toast.makeText(RegistrationActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
else
Toast.makeText(RegistrationActivity.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
});
}
}
});
userLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
});
}
private void setupUIViews() {
userName = (EditText)findViewById(R.id.etUserName);
userEmail = (EditText)findViewById(R.id.etEmail);
password = (EditText)findViewById(R.id.etUserPassword);
regButton = (Button)findViewById(R.id.btnRegister);
userLogin = (TextView)findViewById(R.id.tvUserLogin);
}
private Boolean validate() {
Boolean result = false;
String name = userName.getText().toString();
String email = userEmail.getText().toString();
String passwrd = password.getText().toString();
if(name.isEmpty() || passwrd.isEmpty() || email.isEmpty()) {
Toast.makeText(this,"Please Enter all details", Toast.LENGTH_SHORT).show();
}
else
result = true;
return result;
}
}
这是我的注册活动。我检查了工具 - &gt; firebase - &gt;认证,所有连接都已正确完成。