我是Android应用开发的新手。我想在firebase中添加用户的电子邮件之前发送验证邮件。以下代码中需要进行哪些修改,以便将验证电子邮件发送给用户的电子邮件。
package com.threemusketeers.healthmaster;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
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;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//defining view objects
private EditText editTextEmail;
private EditText editTextPassword;
private Button buttonSignup;
private ProgressDialog progressDialog;
private TextView textViewSignin;
;
//defining firebaseauth object
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing firebase auth object
firebaseAuth = FirebaseAuth.getInstance();
//if getCurrentUser does not returns null
if (firebaseAuth.getCurrentUser() != null) {
//that means user is already logged in
//so close this activity
finish();
//and open profile activity
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
//initializing views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
textViewSignin = (TextView) findViewById(R.id.textViewSignin);
buttonSignup = (Button) findViewById(R.id.buttonSignup);
progressDialog = new ProgressDialog(this);
//attaching listener to button
buttonSignup.setOnClickListener(this);
textViewSignin.setOnClickListener(this);
}
private void registerUser() {
//getting email and password from edit texts
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
//checking if email and passwords are empty
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;
}
//if the email and password are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if (task.isSuccessful()) {
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
//Toast.makeText(MainActivity.this, "Successfully registered", Toast.LENGTH_LONG).show();
} else {
//display some message here
Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
@Override
public void onClick(View view) {
if(view == buttonSignup){
registerUser();
}
if(view == textViewSignin){
//open login activity when user taps on the already registered textview
startActivity(new Intent(this, LoginActivity.class));
}
}
}
答案 0 :(得分:0)
使用新API(&#39; com.google.firebase:firebase-auth:9.6.1&#39; ),您可以使用:
private void verifyEmail(){
firebaseAuth.getCurrentUser().sendEmailVerification()
.addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getActivity(), "Email sent", Toast.LENGTH_LONG).show();
}
}
});
}
答案 1 :(得分:0)
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
user.sendEmailVerification();
if (task.isSuccessful()) {
finish();
Toast.makeText(Register.this, "Registered Successfully. Check your email", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), yourActivity.class));
}
}
});
if (firebaseAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(getApplicationContext(), yourActivity.class));
}
} else {
Toast.makeText(Register.this, "Could not Register User. Try Again", Toast.LENGTH_SHORT).show();
}
}
});
答案 2 :(得分:0)
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/UserInfo
登录活动......
if(user.isEmailVerified){
//allow to login app
} else{
//show toast, help, inform that account is not registered or no stable internet connection
}