如何检索SharedPreferences,哪个从Android中的另一个活动中保存?

时间:2015-03-30 14:18:36

标签: android android-activity sharedpreferences

我的应用中有情况。让我们进行两项活动A& B。 A是主要活动(在打开应用程序时启动)。 B从A开始。我在B中有一个复选框,使用isChecked()方法将布尔值分配给一个名为value&的布尔值。保存到SharedPreferences。保存到SharedPreferences的代码是

SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("value", value);
editor.commit();

现在我想在打开应用程序期间从活动A(即主要活动)中读取此值,并根据此值我需要执行其他操作。

我用来检索值的代码是

SharedPreferences sharedPreference=PreferenceManager.getDefaultSharedPreferences(this);
boolean value=sharedPreferences.getBoolean("value", false);

问题是我无法在活动A中检索此值。由于未检索到值,因此不执行依赖于值的操作。也无论在重新启动应用程序CheckBox时检查checkBox多少次活动中未经检查B.问题是什么?

1 个答案:

答案 0 :(得分:2)

您可以创建管理应用程序首选项的单例类(不要忘记创建自己的YourAppSingleton类):

public final class PreferencesUtil {

    private PreferencesUtil() {}

    public static float getFloatValue(String key, float defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getFloat(key, defaultValue);
    }

    public static void setFloatValue(String key, float value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public static long getLongValue(String key, long defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getLong(key, defaultValue);
    }

    public static void setLongValue(String key, long value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public static void setIntValue(String key, int value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }


    public static int getIntValue(String key, int defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getInt(key, defaultValue);
    }

    public static void setStringValue(String key, String value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getStringValue(String key) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getString(key, null);
    }

    public static String getStringValue(String key, String defValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getString(key, defValue);
    }

    public static boolean getBooleanValue(String key, boolean defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getBoolean(key, defaultValue);
    }

    public static void setBooleanValue(String key, boolean value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void clearValue(String key) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.remove(key);
        editor.commit();
    }

    private static SharedPreferences getSharedPreferences() {
        return YourAppSingleton.getInstance().getSharedPreferences(YourAppSingleton.getInstance().getString(R.string.app_name), Context.MODE_PRIVATE);
    }
}

申请类:

public class YourAppSingleton extends Application {

    private static YourAppSingleton instance = null;

    public void onCreate() { 
        super.onCreate();
        instance = this;
    }

    public static YourAppSingleton getInstance() {
        return instance;
    }
}