简体   繁体   中英

SharedPreferences value always returns true?

I have an Activity in which I am trying to implement a kind of auto-login. In my login activity, I have this:

sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE);
User.setUid(sharedPref.getInt("UID", 1));
Boolean al = sharedPref.getBoolean("AUTOLOGIN", false);

...

if (al) {
    Log.i("AUTOLOGIN", "Go!");
    Gui.createAlert(context, context.getString(R.string.loading));
    Intent i = new Intent(context, CityActivity.class);
    context.startActivity(i);
}
...
loginBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       ...
       Ajax.AjaxListener callback = new Ajax.AjaxListener() {
          int uid = Integer.parseInt(userInfo.optString("user_id", "1"));
          String sid = user.optString("sid", "");
          String k = json.optString("k", "");
          Boolean al = autoLogin.isChecked();

          SharedPreferences.Editor e = sharedPref.edit();
          e.putInt("UID", uid).putBoolean("AUTOLOGIN", al).putString("SID", sid).apply();

          Gui.createAlert(context, context.getString(R.string.loading));
          Intent i = new Intent(context, CityActivity.class);
      };
});

I've only been able to test this on the emulator, and I can't seem to get the SharedPreferences file contents to show in Android Device Monitor, but every time the app loads, it does the autologin routine. Even when I shut down the emulator completely and restart everything the autologin flag seems to persist. I'm pretty new to Java/Android programming, but coming from a PHP and JavaScript background, it's not hard to pick up. I'm just stumped as to why the AUTOLOGIN SharedPreference key always seems to return true when checked. Is there an example of implementing autologin with SharedPreferences ?

I should note that I tried to use a database originally, but scrapped the idea because of difficulty of use and the minimal data required right now.

Try This Hope This Will Help You. MainActivity.java

public class MainActivity extends Activity {

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

    public void login(View v){

        SharedPreferences spf=getSharedPreferences("myprfs",Context.MODE_PRIVATE);
        String name=spf.getString("uname", "no value");

        String pass=spf.getString("pass", "no value");      

        EditText et1=(EditText)findViewById(R.id.editText1);
        EditText et2=(EditText)findViewById(R.id.editText2);


        if(et1.getText().toString().equalsIgnoreCase(name) && et2.getText().toString().equalsIgnoreCase(pass))
        {

            Intent i=new Intent();
            i.setComponent(new ComponentName(getApplicationContext(), WelcomeActivity.class));
            startActivity(i);

        }



    }

    public void register(View v){

        Intent i=new Intent();
        i.setComponent(new ComponentName(getApplicationContext(), RegistrationActivity.class));
        startActivity(i);



    }

}

RegistrationActivity.java

public class RegistrationActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.register);

    }


    public void register(View v){

        EditText et1=(EditText)findViewById(R.id.editText1);
        EditText et2=(EditText)findViewById(R.id.editText2);
        EditText et3=(EditText)findViewById(R.id.editText3);


        SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);

        SharedPreferences.Editor spe=spf.edit();

        spe.putString("uname", et1.getText().toString());
        spe.putString("pass", et2.getText().toString());
        spe.putString("dob", et3.getText().toString());

        spe.commit();

        finish();   

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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