当前位置:主页 > 移动开发 > Android代码 >

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

时间:2021-05-18 09:42:32 | 栏目:Android代码 | 点击:

很多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); 
    } 

您可能感兴趣的文章:

相关文章