2016-07-14 84 views
1

我想訪問共享首選項靜態方式,以避免使用過多的代碼,但是當我閱讀共享首選項,看起來像沒有保存靜態方法「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(); 

其印在日誌:

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 
+3

設置該值後,您忘記調用commit()。嘗試'getSharedPrefferences()。edit()。putBoolean(「DBSyncNeeded」,value).commit();' – Rashin

+1

調用「commit()」工作;) – MikeOx

回答

2

試試這個:

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()方法根據是否完成,返回一個布爾值。 True表示編輯成功,而false表示出錯。

+0

您的努力延長答案是讚賞,我不知道這訪問共享偏好的方式:) – MikeOx

+0

很高興聽到它工作:) – Zoe

2

您需要使用commitapply來實際執行請求。

提交你的喜好改變從這個EditorSharedPreferences對象是編輯。這自動執行 請求的修改,替換當前在 SharedPreferences中的任何內容。

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