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

Why Static Shared Preferences are not saved?

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

我的申请代码:

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

我的喜好activity:

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

然后在另一个 class 我做:

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

并在日志中打印:

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

您需要使用 commit or apply 来实际执行请求。

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

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

试试这个:

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,您可以很容易地知道它是否成功。根据 documentation commit() 方法 returns 一个基于是否完成的布尔值。 True 表示编辑成功,false 表示出错。