为什么不保存静态共享首选项?

时间:2016-07-14 12:50:42

标签: android static sharedpreferences static-methods

我想以静态的方式访问共享首选项以避免使用过多的代码,但是当我读取共享首选项时,看起来好像没有保存静态方法" setSyncDBIsNeeded()",我是什么'我做错了吗?

MyApplication代码:

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .name(Realm.DEFAULT_REALM_NAME)
                .schemaVersion(0)
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);

    }

    public static Context getContext() {
        return instance.getApplicationContext();
    }
}

我的偏好活动:

public class PreferenceController {
    SharedPreferences sharedPreferences;
    private static String project = "com.example.myproject";

    public PreferenceController() {
        sharedPreferences = MyApplication.getContext().getSharedPreferences(project, Context.MODE_PRIVATE);
    }

    public PreferenceController(Context context) {
        sharedPreferences = context.getSharedPreferences(project, Context.MODE_PRIVATE);
    }

    /* getters and setters */

    // Static methods

    public static void setSyncDBIsNeeded(boolean value) {
        Log.d("PREFCON","Setted DBSyncNeeded : "+value);
        getSharedPrefferences().edit().putBoolean("DBSyncNeeded", value);
    }

    public static boolean getSyncDBIsNeeded() {
        Log.d("PREFCON","DBSyncNeeded: "+getSharedPrefferences().getBoolean("DBSyncNeeded", false));
        return getSharedPrefferences().getBoolean("DBSyncNeeded", false);
    }

    private static SharedPreferences getSharedPrefferences() {
        return MyApplication.getContext().getSharedPreferences(project, Context.MODE_PRIVATE);
    }
}

然后在另一堂课中我做了:

PreferenceController.setSyncDBIsNeeded(true);
PreferenceController.getSyncDBIsNeeded();

并将其打印在Log:

07-14 14:24:04.665 27658-27658/com.example.myproject D/PREFCON: Setted DBSyncNeeded : true
07-14 14:24:04.665 27658-27658/com.example.myproject D/PREFCON: DBSyncNeeded: false

2 个答案:

答案 0 :(得分:3)

试试这个:

SharedPreferences.Editor editor = getSharedPrefferences().edit();
editor.putBoolean("DBSyncNeeded", value);
editor.commit();

您必须记住更新对SharedPreferences所做的更改,以便SharedPreferences实际保存它。

插入您的代码:

public static void setSyncDBIsNeeded(boolean value) {
    Log.d("PREFCON","Setted DBSyncNeeded : "+value);
    SharedPreferences.Editor editor = getSharedPrefferences().edit();
    editor.putBoolean("DBSyncNeeded", value);
    boolean completed = editor.commit();
    Log.e("PREFCON", "Updating SharedPreferences was " + completed + "!";
}

通过添加要设置为editor.commit的布尔值,您可以轻松地知道它是否成功。根据{{​​3}},commit()方法返回一个布尔值,基于它是否完成。 True表示编辑成功,而false表示出错。

答案 1 :(得分:2)

您需要使用commitapply来实际执行请求。

  

将您的偏好设置更改从此Editor返回到。{   它正在编辑的SharedPreferences个对象。这原子地执行   要求修改,替换当前的任何内容   SharedPreferences

public static void setSyncDBIsNeeded(boolean value) {
    Log.d("PREFCON","Setted DBSyncNeeded : "+value);
    getSharedPrefferences().edit().putBoolean("DBSyncNeeded", value).apply();
}