将布尔值从注册发送到登录,但 toast 不工作
Send boolean value from register to login but toast is not working
我正在使用正确的凭据注册用户,注册完成后立即进入登录 activity。我只想在成功注册后显示祝酒词。但我不确定我在这里做错了什么是放额外的还是额外的布尔值?或者我的逻辑在这里用错了。为了登录成功,需要使用 firebase 进行验证,但我需要显示一条提示消息,以便用户知道。
这是我的RegisterActivity.class
public void register(final String username, final String phoneNo, String email, String password) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser firebaseUser = auth.getCurrentUser();
assert firebaseUser != null;
final String userID = firebaseUser.getUid();
firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "You have registered successfully!", Toast.LENGTH_SHORT).show();
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);
HashMap<String, Object> map = new HashMap<>();
map.put("id", userID);
map.put("username", username.toLowerCase());
map.put("phoneNo", phoneNo);
map.put("onlineStatus", "online"); //will add later (e.g. edit profile)
map.put("typingTo", "noOne"); //will add later (e.g. edit profile)
map.put("imageurl", "gs://araba-92732.appspot.com/placeholder.png");
map.put("about", "");
reference.setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
pd.dismiss();
//The problem resides here
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.putExtra("isRegister", false);
startActivity(intent);
auth.signOut();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
});
} else {
pd.dismiss();
Toast.makeText(RegisterActivity.this, Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
pd.dismiss();
Toast.makeText(RegisterActivity.this, "You can't register with this email or password", Toast.LENGTH_SHORT).show();
}
}
});
}
这是我的LoginActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
txt_signup = findViewById(R.id.txt_signup);
RecoverPassword = findViewById(R.id.recoverPassTv);
///The problem resides here
Intent intent = getIntent();
boolean registers = intent.getBooleanExtra("isRegister", false);
if(!registers)
{
Toast.makeText(this, "You need to verify your email address before signing in!", Toast.LENGTH_LONG).show();
}
auth = FirebaseAuth.getInstance();
txt_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ProgressDialog pd = new ProgressDialog(LoginActivity.this);
pd.setMessage("Please wait...");
pd.show();
String str_email = email.getText().toString();
String str_password = password.getText().toString();
if (TextUtils.isEmpty(str_email) || TextUtils.isEmpty(str_password)) {
Toast.makeText(LoginActivity.this, "All fields are required!", Toast.LENGTH_SHORT).show();
} else {
auth.signInWithEmailAndPassword(str_email, str_password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users")
.child(auth.getCurrentUser().getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
pd.dismiss();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
finish();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
pd.dismiss();
}
});
} else {
pd.dismiss();
Toast.makeText(LoginActivity.this, "Please, verify your e-mail address", Toast.LENGTH_SHORT).show();
}
} else {
pd.dismiss();
Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
RecoverPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRecoverPasswordDialog();
}
});
}
用过这个
Intent intent = getIntent();
boolean registers = getIntent().getExtras().getBoolean("isRegister");
if(!registers)
{
Toast.makeText(LoginActivity.this, "You need to verify your email address before signing
in!", Toast.LENGTH_LONG).show();
}
我正在使用正确的凭据注册用户,注册完成后立即进入登录 activity。我只想在成功注册后显示祝酒词。但我不确定我在这里做错了什么是放额外的还是额外的布尔值?或者我的逻辑在这里用错了。为了登录成功,需要使用 firebase 进行验证,但我需要显示一条提示消息,以便用户知道。
这是我的RegisterActivity.class
public void register(final String username, final String phoneNo, String email, String password) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser firebaseUser = auth.getCurrentUser();
assert firebaseUser != null;
final String userID = firebaseUser.getUid();
firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "You have registered successfully!", Toast.LENGTH_SHORT).show();
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);
HashMap<String, Object> map = new HashMap<>();
map.put("id", userID);
map.put("username", username.toLowerCase());
map.put("phoneNo", phoneNo);
map.put("onlineStatus", "online"); //will add later (e.g. edit profile)
map.put("typingTo", "noOne"); //will add later (e.g. edit profile)
map.put("imageurl", "gs://araba-92732.appspot.com/placeholder.png");
map.put("about", "");
reference.setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
pd.dismiss();
//The problem resides here
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.putExtra("isRegister", false);
startActivity(intent);
auth.signOut();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
});
} else {
pd.dismiss();
Toast.makeText(RegisterActivity.this, Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
pd.dismiss();
Toast.makeText(RegisterActivity.this, "You can't register with this email or password", Toast.LENGTH_SHORT).show();
}
}
});
}
这是我的LoginActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
txt_signup = findViewById(R.id.txt_signup);
RecoverPassword = findViewById(R.id.recoverPassTv);
///The problem resides here
Intent intent = getIntent();
boolean registers = intent.getBooleanExtra("isRegister", false);
if(!registers)
{
Toast.makeText(this, "You need to verify your email address before signing in!", Toast.LENGTH_LONG).show();
}
auth = FirebaseAuth.getInstance();
txt_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ProgressDialog pd = new ProgressDialog(LoginActivity.this);
pd.setMessage("Please wait...");
pd.show();
String str_email = email.getText().toString();
String str_password = password.getText().toString();
if (TextUtils.isEmpty(str_email) || TextUtils.isEmpty(str_password)) {
Toast.makeText(LoginActivity.this, "All fields are required!", Toast.LENGTH_SHORT).show();
} else {
auth.signInWithEmailAndPassword(str_email, str_password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users")
.child(auth.getCurrentUser().getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
pd.dismiss();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
finish();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
pd.dismiss();
}
});
} else {
pd.dismiss();
Toast.makeText(LoginActivity.this, "Please, verify your e-mail address", Toast.LENGTH_SHORT).show();
}
} else {
pd.dismiss();
Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
RecoverPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRecoverPasswordDialog();
}
});
}
用过这个
Intent intent = getIntent();
boolean registers = getIntent().getExtras().getBoolean("isRegister");
if(!registers)
{
Toast.makeText(LoginActivity.this, "You need to verify your email address before signing
in!", Toast.LENGTH_LONG).show();
}