一.将登录手机号保存在SharedPreferences中
//"实例化"sharedpreferences和editor
//MODE_PRIVATE代表只能用该APP读取
SharedPreferences share = getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = share.edit();
editor.putString("phone",mEtUser.getText().toString());
editor.putBoolean("LoginBool", true);
editor.apply();
二.在onCreate中判断SharedPreferences中是否有数据,如果有的话自动跳转
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences share = getSharedPreferences("Login", Context.MODE_PRIVATE);
//当share有数据并且Boolean为true时,才可以跳转
if(share!=null&&share.getBoolean("LoginBool",true)){
//得到保存在SharedPreferences中的phone
String strAccount = share.getString("phone", "");
Intent intent = null;
intent = new Intent(MainActivity.this, SlideActivity.class);
intent.putExtra("extra_name", strAccount);
startActivity(intent);
ActivityCollector.finishAll();
}
}
三.当退出登录或注销账号时将Boolean设为false,使得不能再自动登录
SharedPreferences share = getSharedPreferences("Login", Context.MODE_PRIVATE);
share.edit().putBoolean("LoginBool", false).apply();