关闭应用程序后阻止用户再次登录 [英] Preventing users to login again after closing the app
本文介绍了关闭应用程序后阻止用户再次登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的登录页面有问题.成功登录我的帐户后,关闭该应用程序并重新打开它,该屏幕仍在登录页面上.如何使我的应用像Facebook?我已经尝试实现SharedPreferences
,但是每次用户成功登录时,我都会传递IntentExtra
.
I have a problem in my login page. after successful login my account, and close the app and re-open it, The screen is still on the login page. How can I make my app like Facebook? I have tried to implement SharedPreferences
but I'm passing an IntentExtra
every time the user has successfully logged in.
谢谢
添加代码的屏幕截图
推荐答案
您可以创建这样的共享首选项类:-
you can create a shared preference class like this:-
public class AppPrefrences {
private static SharedPreferences mPrefs;
private static SharedPreferences.Editor mPrefsEditor;
public static boolean isUserLoggedOut(Context ctx) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
return mPrefs.getBoolean("id_logged_in", true);
}
public static void setUserLoggedOut(Context ctx, Boolean value) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putBoolean("id_logged_in", value);
mPrefsEditor.commit();
}
public static String getUserName(Context ctx) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
return mPrefs.getString("userName", "");
}
public static void setUserName(Context ctx, String value) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putString("userName", value);
mPrefsEditor.commit();
}
public static void clearAllPreferences(Context ctx) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mPrefsEditor = mPrefs.edit();
mPrefsEditor.clear();
mPrefsEditor.commit();
}
}
,当您登录您的应用程序时,请像下面这样设置值:-
and when you login in your app then set the value like this:-
setUserLoggedOut(YourActivity.class, false);
并在启动屏幕上设置如下所示:-
and set a check on splash screen like this:-
if (isUserLoggedOut(StartActivity.this)) {
startActivity(new Intent(StartActivity.this, LoginActivity.class));
finish();
} else {
startActivity(new Intent(StartActivity.this, MainActivity.class));
finish();
}
这篇关于关闭应用程序后阻止用户再次登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文