简体   繁体   English

用户使用SharedPreferences保持登录android应用程序不起作用

[英]User keep logged in android app using SharedPreferences not working

We have seen in almost every app that the user logs in for the first time and the next time he opens the app, he goes straight inside the app without authentication. 我们几乎在每个应用程序中都看到该用户首次登录,而下次他打开该应用程序时,他直接进入该应用程序而无需身份验证。 So I am trying to create exact same feature into my app, I followed documentation and coded into my app. 因此,我试图在我的应用程序中创建完全相同的功能,我按照文档进行了编码,并编码到我的应用程序中。 Build is successful but it's not working, it keeps asking for authentication after closing app. 构建成功,但无法正常工作,在关闭应用程序后不断要求进行身份验证。

Code

package abc.xyz;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    Button b;
    SharedPreferences sp;
    EditText username,password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button)findViewById(R.id.button);
        username = (EditText)findViewById(R.id.editText);
        password = (EditText)findViewById(R.id.editText2);
        sp = getSharedPreferences("b", MODE_PRIVATE);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                login();

                if(sp.getBoolean("Logged",false)){
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    startActivity(intent);
                }
            }
        });

    }
    public void login(){


        String user=username.getText().toString().trim();
        String pass=password.getText().toString().trim();
        if(user.equals("admin")&& pass.equals("admin"))
        {
            sp.edit().putBoolean("Logged", true).apply();
            Toast.makeText(this,"Success!",Toast.LENGTH_LONG).show();
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);

        }else {
            Toast.makeText(this,"username and password do not matched!",Toast.LENGTH_LONG).show();
        }
    }

}

Can anyone help me out here? 有人可以帮我从这里出去吗?

Your are saving the state as true then every time executing the login method instead you need to move to MainActivity2 when the value is true so use 您将状态保存为true然后每次执行login方法时,则需要在值为true时移动到MainActivity2 ,因此请使用

// move to next activity if user is authenticated
if(sp.getBoolean("Logged",false)){
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);
}
b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                login();
            }
        });

Note : as mentioned by @shb , case sensitivity matter 注意:如@shb所述,区分大小写很重要

You are saving 您正在保存

sp.edit().putBoolean("Logged", true).apply();

with an uppercase 'L' //Logged 大写的'L'//已记录

but while retrieving you're using a lowercase 'l' //logged 但是在检索时,您使用的是小写字母'l'///

if(sp.getBoolean("logged",false)){
//...
}

use either "logged" or "Logged" in both of the places. 在两个地方都使用“已记录”或“已记录”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM