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

    xiaoxiao2021-03-25  102

    很多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(); //将登陆状态设置为true; intent = new Intent(this,Activity2.class); }

    转载请注明原文地址: https://ju.6miu.com/read-20342.html

    最新回复(0)