且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关闭应用程序后阻止用户再次登录

更新时间:2023-10-13 12:22:34

您可以创建这样的共享首选项类:-

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();
                }