面临有关Android中的改装登录的问题

时间:2018-11-08 11:30:05

标签: java android login retrofit2

我遇到翻新登录的问题。当我在手机中运行该应用程序时,我以注册用户身份登录...登录成功...但是当我注销并要使用另一个帐户重新登录时,“该应用程序使用先前的帐户登录了我” ...我不明白这是什么问题,请帮忙。 我不明白为什么我的应用程序会出现此问题。

注意:我在应用程序中使用翻新(带有Firebase)进行注册和登录。 谢谢

这是我的登录课程的代码

    private Button forgot ;
    private TextView CreactAccount_text;
    private EditText login_email , login_password;
    private ProgressDialog mLoginProgress;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        Toolbar toolbar = (Toolbar) findViewById(R.id.login_page_toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar()!= null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        FirebaseApp.initializeApp(this);
//        Realm.init(this);
        // User Session Manager



        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.setStatusBarColor(getColor(R.color.login_statusbar_color));
        }
        else {
            window.setStatusBarColor(getResources().getColor(R.color.login_statusbar_color));
        }

        mLoginProgress = new ProgressDialog(this);


        CreactAccount_text = (TextView) findViewById(R.id.ui_crateaccount_text);
        forgot = (Button) findViewById(R.id.ui_btn_forgot);
        forgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent forgot_password = new Intent(Login_Activity.this , Forgot_Password_Activity.class);
                startActivity(forgot_password);
            }
        });
        CreactAccount_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent move_signup = new Intent(Login_Activity.this , Signup_Activity.class);
                startActivity(move_signup);
            }
        });

        login_email = (EditText) findViewById(R.id.ui_login_email);
        login_password = (EditText) findViewById(R.id.ui_login_password);
        findViewById(R.id.ui_signin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                usersignin();
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home)
            finish();
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(
                R.anim.no_anim, R.anim.slide_right_out);
        AppUtils.hideSoftKeyboard(this);
    }

    public void usersignin(){


        final String email = login_email.getText().toString().trim();
        final String password = login_password.getText().toString().trim();


        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
            login_email.setError("Email is not correctly formated");
            login_email.requestFocus();
            return;
        }
        if (password.isEmpty()){
            login_password.setError("password is required");
            login_password.requestFocus();
            return;
        }
        if (password.length() < 6){
            login_password.setError("Password should be atleast 6 characters");
            login_password.requestFocus();
            return;

        }


        if(!TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)){

            mLoginProgress.setTitle("Logging In");
            mLoginProgress.setMessage("Please wait while we check your credentials.");
            mLoginProgress.setCanceledOnTouchOutside(false);
            mLoginProgress.show();

            SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME,email);
            SharedPreferenceUtil.storeStringValue(Login_Activity.this,Constants.PASSWORD,password);

            RetrofitUtil.createProviderAPI().userLogin(email , password).enqueue(loginUser(this));



        }


    }
    @Override
    public void onLoginUser(RetrofitClientLogin data) {
        if(data.getType().equals(Constants.SUCCESS)){

            FirebaseInstanceId.getInstance().getToken();

            UtilFirebaseAnalytics.logEvent(Constants.EVENT_LOGIN,Constants.KEY_EMAIL,login_email.getText().toString());
            SharedPreferenceUtil.storeBooleanValue(this,Constants.ISUSERLOGGEDIN,true);
            if(getIntent() != null && getIntent().getBooleanExtra(Constants.IS_RESULT_ACTIVITY,false)){
                setResult(RESULT_OK);
            }else{
                hideProgressDialog();
                openAcitivty(Home_Activity.class);

            }
            loginOnFirebase();

        }
    }



    private void loginOnFirebase(){
        final FirebaseAuth mAuth = FirebaseAuth.getInstance();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if(currentUser == null){
            final String email = login_email.getText().toString();
            final String password = login_password.getText().toString();
            mAuth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (!task.isSuccessful()) {
                                mAuth.createUserWithEmailAndPassword(email, password)
                                        .addOnCompleteListener(Login_Activity.this, new OnCompleteListener<AuthResult>() {
                                            @Override
                                            public void onComplete(@NonNull Task<AuthResult> task) {
                                                hideProgressDialog();
                                                finish();
                                            }
                                        });
                            }else{
                                hideProgressDialog();
                                finish();
                            }
                        }
                    });
        }

    }


    protected void openAcitivty(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    protected void showProgressDialog(String msg) {
        try {
            if (mLoginProgress != null && !mLoginProgress.isShowing()) {
                mLoginProgress.setMessage(msg);
                mLoginProgress.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void hideProgressDialog() {
        try {
            if (mLoginProgress != null && mLoginProgress.isShowing()) {
                mLoginProgress.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSignup(RetrofitClientLogin data) {

    }


    @Override
    public void OnError(String error) {
        hideProgressDialog();
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();

    }

1 个答案:

答案 0 :(得分:0)

您正在使用SharedPreferences来存储登录凭据。用户输入有效的登录凭据(登录ID和密码)后,您可以通过usersignin()方法将其存储在SharedPreferences中。

SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME, email);
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.PASSWORD, password);

现在,我假设(您只发布了LoginActivity,没有提及其他部分),您的应用具有启动页面。出现在登录页面之前。您可以在其中确定用户是否已经登录(使用SharedPreference值)。如果是,请重定向到“主页”或“登录”页面。

此问题的解决方法非常简单。当用户从应用程序注销时,您还需要清除SharedPreferences。

喜欢这个

SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME, null);
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.PASSWORD, null);