软件编程
位置:首页>> 软件编程>> Android编程>> Android 使用SharePerference判断是否为第一次登陆的实现代码

Android 使用SharePerference判断是否为第一次登陆的实现代码

作者:BrcLi  发布时间:2021-07-31 18:51:32 

标签:android,shareperference,登陆

很多app中在第一次安装登陆时会有引导欢迎界面,第二次打开时就不再显示引导页面。

这个功能可以通过使用SharePreferences将用户的登陆信息保存起来,当app启动时判断登陆信息决定打开页面。

以下是创建的SharePreference类:


public class SharePreference {
 Context context;
 public SharePreference(Context context)
 {
   this.context = context;
 }
 /****设置状态  false为安装后第一次登录,true为已经登录过****/
 public void setState()
 {
   SharedPreferences sp = context.getSharedPreferences("save.himi", Context.MODE_PRIVATE);
   Editor editor = sp.edit();
   editor.putBoolean("isLogin", true);
   editor.commit();
 }
 /***获取状态***/
 public boolean getState()
 {
   SharedPreferences sp = context.getSharedPreferences("save.himi", Context.MODE_PRIVATE);
   boolean b = sp.getBoolean("isLogin", false);
   return b;
 }
}

在app打开前可以获取登陆状态,选择展示界面:


isLogin = sp.getState();
   if(isLogin){
      intent = new Intent(this,Activity1.class);
   }
   else {
     sp.setState();<span style="white-space:pre">  </span>//将登陆状态设置为true;
      intent = new Intent(this,Activity2.class);
   }

以上所述是小编给大家介绍的Android 使用SharePerference判断是否为第一次登陆的实现代码网站的支持!

来源:http://blog.csdn.net/brcli/article/details/61195534

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com