Android 应用程序终止时如何保留 SharedPreferences 值

How To Keep SharedPreferences Values When Android App Killed

我正在做一个 android 项目。我想在 android 应用程序终止时推送通知。因此,我正在创建 BroadcastReceiver 和服务 class。他们在工作。但是,我有一个问题。

这个问题是,当用户登录我的应用程序时;我用 SharedPreferences 存储这个值。但是,当我终止应用程序时,此 SharedPreferences 取 "null" 值。

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference

当我搜索时,人们说:"editor.clear();" 但它不起作用。我能做什么?

我的登录 Class:

public static SharedPreferences UserDetailsSP,

UserDetailsSP = getSharedPreferences("UserDetails", MODE_PRIVATE);
SharedPreferences.Editor UserDetailsPreferencessEdit = UserDetailsSP .edit();
UserDetailsPreferencessEdit.clear();
UserDetailsPreferencessEdit.putString("UserName", A);
UserDetailsPreferencessEdit.commit();

我这样称呼其他 java class:

String a1 = Login.UserDetailsSP.getString("UserName","A");
You this approach create a SessionManager class
public class AppPrefrences {

        private static SharedPreferences mPrefs;
        private static SharedPreferences.Editor mPrefsEditor;
public AppPrefrences(Context context)
{
    int PRIVATE_MODE = 0;
    pref = context.getSharedPreferences("UserDetails", PRIVATE_MODE);
    editor = pref.edit();
    editor.apply();
}

        public static boolean isUserLoggedOut() {

            return mPrefs.getBoolean("id_logged_in", false);
        }

        public static void setUserLoggedOut(Boolean value) {

            mPrefsEditor.putBoolean("id_logged_in", value);
            mPrefsEditor.commit();
        }

public static String getUserName() {

        return mPrefs.getString("userName", "");
    }

    public static void setUserName(String value) {

        mPrefsEditor.putString("userName", value);
        mPrefsEditor.commit();
    }

    public static void clearAllPreferences()
    {

       mPrefsEditor.clear();
       mPrefsEditor.commit();
   }
  }


Inside your activity
AppPrefrences sess = new AppPrefrences(this);
sess.setUserName("A");

您可以像这样创建共享首选项 class:-

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

当您登录您的应用程序时,然后像这样设置值:-

setUserLoggedOut(YourActivity.class, false);

并像这样检查启动画面:-

if (isUserLoggedOut(StartActivity.this)) {
                    startActivity(new Intent(StartActivity.this, LoginActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(StartActivity.this, MainActivity.class));
                    finish();
                }