Android 使用SharePerference判断是否为第一次登陆的实现代码
很多app中在第一次安装登陆时会有引导欢迎界面,第二次打开时就不再显示引导页面。
这个功能可以通过使用SharePreferences将用户的登陆信息保存起来,当app启动时判断登陆信息决定打开页面。
以下是创建的SharePreference类:
publicclassSharePreference{ Contextcontext; publicSharePreference(Contextcontext) { this.context=context; } /****设置状态false为安装后第一次登录,true为已经登录过****/ publicvoidsetState() { SharedPreferencessp=context.getSharedPreferences("save.himi",Context.MODE_PRIVATE); Editoreditor=sp.edit(); editor.putBoolean("isLogin",true); editor.commit(); } /***获取状态***/ publicbooleangetState() { SharedPreferencessp=context.getSharedPreferences("save.himi",Context.MODE_PRIVATE); booleanb=sp.getBoolean("isLogin",false); returnb; } }
在app打开前可以获取登陆状态,选择展示界面:
isLogin=sp.getState(); if(isLogin){ intent=newIntent(this,Activity1.class); } else{ sp.setState();//将登陆状态设置为true; intent=newIntent(this,Activity2.class); }
以上所述是小编给大家介绍的Android使用SharePerference判断是否为第一次登陆的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!