登录应用程序时出现问题

问题描述 投票:-4回答:1

我有一个关于登录的问题..!

package com.example.myapplication;

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.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity

   {
       private Button CreateAccountButton;
       private EditText InputName, InputPhoneNumber, InputPassword;
       private ProgressDialog loadingBar;
       FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState)
            {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        CreateAccountButton = (Button) findViewById(R.id.main_register_btn);
        InputName = (EditText) findViewById(R.id.register_username_input);
        InputPassword = (EditText) findViewById(R.id.register_password_input);
        InputPhoneNumber = (EditText) findViewById(R.id.register_phone_number_input);
        loadingBar = new ProgressDialog(this);
        mAuth = FirebaseAuth.getInstance();
        

        CreateAccountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                CreateAccount();
                

            }
        });

    }

       private void CreateAccount()
       {
           String name = InputName.getText().toString();
           String phone = InputPhoneNumber.getText().toString();
           String password = InputPassword.getText().toString();
           if (TextUtils.isEmpty(name))
           {
               Toast.makeText(this, "please enter your name", Toast.LENGTH_SHORT).show();
           }
          else if (TextUtils.isEmpty(phone))
           {
               Toast.makeText(this, "please enter your phone number", Toast.LENGTH_SHORT).show();
           }
          else if (TextUtils.isEmpty(password))
           {
               Toast.makeText(this, "please enter your password", Toast.LENGTH_SHORT).show();
           }
           else
           {
               loadingBar.setTitle("Create Account");
               loadingBar.setMessage("Please wait, While we are checking the credentials");
               loadingBar.setCanceledOnTouchOutside(false);
               loadingBar.show();

               validatephonenumber(name, phone, password);

           }


       }

       private void validatephonenumber(final String name, final String phone, final String password)
       {
           final DatabaseReference RootRef;
           RootRef = FirebaseDatabase.getInstance().getReference();

           RootRef.addListenerForSingleValueEvent(new ValueEventListener()
            {
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot)
               {
                  if(dataSnapshot.child("Users").child(phone).exists())
                  { Toast.makeText(RegisterActivity.this, "this" +phone+ "alredy exists", Toast.LENGTH_SHORT).show();
                      loadingBar.dismiss();
                      Toast.makeText(RegisterActivity.this, "Please try using another phone number", Toast.LENGTH_SHORT).show();

                      Intent intent= new Intent(RegisterActivity.this, MainActivity.class);
                      startActivity(intent);

                  }
                  else
                  {

                      HashMap<String, Object> userdataMap = new HashMap<>();
                      userdataMap.put("Phone", phone);
                      userdataMap.put("password", password);
                      userdataMap.put("name", name);

                      RootRef.child("User").child(phone).updateChildren(userdataMap)
                              .addOnCompleteListener(new OnCompleteListener<Void>() {
                                  @Override
                                  public void onComplete(@NonNull Task<Void> task)
                                  {
                                      if (task.isSuccessful())
                                      {
                                          Toast.makeText(RegisterActivity.this, "Congratulations your account has been created ", Toast.LENGTH_SHORT).show();
                                          loadingBar.dismiss();

                                          Intent intent= new Intent(RegisterActivity.this, loginActivity.class);
                                          startActivity(intent);
                                      }

                                      else
                                      {
                                          loadingBar.dismiss();
                                          Toast.makeText(RegisterActivity.this, "Network Error: Please try again", Toast.LENGTH_SHORT).show();
                                      }



                                  }
                              });
                  }

               }

               @Override
               public void onCancelled(@NonNull DatabaseError databaseError) {

               }
           });

       }
   }

当我使用此代码并使用电话号码登录时,它应该实际提示“此号码已存在”但它无法正常工作,并且在使用相同的号码再次登录时,所有旧的名称和密码都在换成新的....我怎么能解决这个问题???

java android firebase firebase-authentication
1个回答
0
投票

尝试删除你的意图流程; -

Intent intent= new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
© www.soinside.com 2019 - 2024. All rights reserved.