2016-06-28 84 views
-2

我有一個sharedpreferences,它保存和檢索數據正確。但問題是這個時候手機重新啓動它給空值。android sharedpreferences當應用程序或手機重新啓動時得到空

這裏是代碼,我有嘗試

if(encodedImage_profile3!=null) 
     { 

      wallpaper_sharedprefrences_profile3 = PreferenceManager.getDefaultSharedPreferences(context); 
      SharedPreferences.Editor edit = wallpaper_sharedprefrences_profile3.edit(); 
      edit.putString(PROFILE2_WALLPAPER, encodedImage_profile3); 
      edit.commit(); 
      // Toast.makeText(context, "new same image in prefrences", Toast.LENGTH_SHORT).show(); 
     } 
     wallpaper_sharedprefrences_profile3 = PreferenceManager.getDefaultSharedPreferences(context); 

     final String load_wallpaper_profile3 =wallpaper_sharedprefrences_profile3.getString(PROFILE2_WALLPAPER, ""); 

我也試試這個

if(encodedImage_profile3!=null) 
     { 
      wallpaper_sharedprefrences_profile3 = context.getSharedPreferences(PREF_PROFILE2_WALLPAPER, Context.MODE_PRIVATE); 
      SharedPreferences.Editor edit = wallpaper_sharedprefrences_profile3.edit(); 
      edit.putString(PROFILE2_WALLPAPER, encodedImage_profile3); 
      edit.commit(); 
     } 
     wallpaper_sharedprefrences_profile3 = context.getSharedPreferences(PREF_PROFILE2_WALLPAPER, Context.MODE_PRIVATE); 
     final String load_wallpaper_profile3 =wallpaper_sharedprefrences_profile3.getString(PROFILE2_WALLPAPER, ""); 
+0

你需要設置申請一次完成編輯共享偏好..... – silverFoxA

+0

當你明確地將默認值設置爲'「」'時,它是如何給出'null'值的?請發佈您的logcat輸出。 – earthw0rmjim

+0

@silverFoxA他稱'commit()'。 – earthw0rmjim

回答

0

而不是硬編碼共享偏好邏輯,創建一個實用工具類相似的下面和訪問您的項目。 Separate methods are available for different data types

/** 
* Shared preference class to handle app's local content 
*/ 
public class SharedPreferenceHandler { 

    //Class variables 
    private SharedPreferences sharedPrefSettings; 
    private SharedPreferences.Editor editor; 
    private static SharedPreferenceHandler m_cPrefObj=null; 

    private SharedPreferenceHandler(Context context){ 
     sharedPrefSettings = context.getSharedPreferences(SharedPreferenceConstants.SHARED_PREF_KEY, Context.MODE_PRIVATE); 
     editor=sharedPrefSettings.edit(); 
    } 

    public static SharedPreferenceHandler getSharedPrefInstance(Context context){ 
     if(m_cPrefObj==null){ 
      m_cPrefObj=new SharedPreferenceHandler(context); 
     } 
     return m_cPrefObj; 
    } 
    /** 
     returns string value for shared preference key 
    */ 
    public String getSharedPrefValue(String key){ 
     return sharedPrefSettings.getString(key,null); 
    } 

    /** 
     returns boolean shared preference value for key 
    */ 
    public boolean getSharedPrefValueBool(String key){ 
     return sharedPrefSettings.getBoolean(key, false); 
    } 
    /** 
     sets string shared preference value for key 
    */ 
    public void setSharedPrefBool(String key,boolean value){ 
     editor.putBoolean(key,value); 
     editor.commit(); 
    } 
    /** 
     sets string shared preference value for key 
    */ 
    public void setSharedPref(String key,String value){ 
     editor.putString(key, value); 
     editor.commit(); 
    } 

    /** 
    * 
    * @param key 
    * @return 
    */ 
    public long getSharedPrefValueLong(String key){ 
     return sharedPrefSettings.getLong(key,0); 
    } 

    /** 
    * 
    * @param key 
    * @param value 
    */ 
    public void setSharedPrefLong(String key,long value){ 
     editor.putLong(key,value); 
     editor.commit(); 
    } 

    /** 
    * 
    * @param key 
    * @return 
    */ 
    public int getSharedPrefValueInt(String key){ 
     return sharedPrefSettings.getInt(key,0); 
    } 

    /** 
    * 
    * @param key 
    * @param value 
    */ 
    public void setSharedPrefInt(String key,int value){ 
     editor.putInt(key,value); 
     editor.commit(); 
    } 
} 

中序中所需的類訪問此邏輯,

private SharedPreferenceHandler mSharedPreferenceHandler; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     initiateDependencies(); 

    } 
    /** 
    * Should initiate classes and objects which will be referred throughout app 
    */ 
    private void initiateDependencies(){ 

     mSharedPreferenceHandler =SharedPreferenceHandler.getSharedPrefInstance(this); 

    } 

要設置共享優先值請執行下列操作

mSharedPreferenceHandler.setSharedPref("PREF_KEY_REFRESH_INTERVAL", "abc"); 
mSharedPreferenceHandler.setSharedPrefBool("PREF_KEY_REFRESH_ACTION",true); 
相關問題