1.免登录验证
【1】把输入的数据提交
String tel = edit_tel.getText().toString().trim(); String pasd = edit_pasd.getText().toString().trim();
@SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor=sp.edit(); editor.putString("USER_TEL", tel); editor.putString("PASSWORD",pasd); editor.commit();
【2】在oncreate里面调取
//免登录验证 sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE); editor_tel=sp.getString("USER_TEL",null); editor_pasd=sp.getString("PASSWORD",null); Log.d("00147","editor_tel is :"+editor_tel+" , editor_pasd is "+editor_pasd); Boolean islogin=TextUtils.isEmpty(editor_tel) || TextUtils.isEmpty(editor_pasd); Log.d("00148","islogin(是否为空) is :"+islogin); if(!islogin){ Intent intent=new Intent(LoginActivity.this,MainActivity.class); intent.putExtra("login_tel", editor_tel);//传值过去 startActivity(intent); }
在这里就是取出来我们存入的值,那如果是第一次登录的话,取出来的是空值,在这里做了一个判断,第一次登录就依旧是登录界面,如果是二次登录(免登录),取出来的值一定不为空,那么直接跳转到主界面了,这里需要注意的就是根据自身需要进行传值。
【3】退出登录
SharedPreferences sharedPreferences=getSharedPreferences("userInfo",MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.clear(); editor.commit(); Intent intent=new Intent(MainActivity.this,LoginActivity.class); startActivity(intent);
把之前存的值clear清空提交,然后跳转到登记页面重新进行登录