2011-09-20 74 views
7

有沒有辦法複製或複製SharedPreference?或者我需要從一個變量中獲取每個變量,然後將它們放入另一個變量中?Android:複製/複製SharedPreferences

+0

從一個變量中獲取每個變量並將它們放入另一個變量是我知道的唯一方法。但SharedPrefs存儲爲一個xml文件,我想你可能能夠複製整個文件並以某種方式粘貼一個新名稱。雖然這種方法可能需要一個有根的設備才能夠將輸入和輸出流設置爲您的應用程序SharedPreferences文件夾。 – FoamyGuy

+0

你爲什麼要複製共享首選項?更詳細地解釋你試圖達到的目標,它將幫助我們提供一個合適的答案。 – Kenny

+0

我的應用將它的變量存儲在sharedpreference中。我有大約50個不斷變化的變量,換句話說,他們不能在應用程序中進行硬編碼。我希望能夠將這些變量放在一邊,以便應用程序用戶可以開始一個新的會話,然後在兩者之間進行切換。我想我可以把它吸了出來,寫出所有的變量到另一個sharedpreference,但如果我可以這樣做,會更容易:savedSharedPreference = sharedPreference。 LoL – cerealspiller

回答

12

嘗試這樣:

//sp1 is the shared pref to copy to 
SharedPreferences.Editor ed = sp1.edit(); 
SharedPreferences sp = Sp2; //The shared preferences to copy from 
ed.clear(); // This clears the one we are copying to, but you don't necessarily need to do that. 
//Cycle through all the entries in the sp 
for(Entry<String,?> entry : sp.getAll().entrySet()){ 
Object v = entry.getValue(); 
String key = entry.getKey(); 
//Now we just figure out what type it is, so we can copy it. 
// Note that i am using Boolean and Integer instead of boolean and int. 
// That's because the Entry class can only hold objects and int and boolean are primatives. 
if(v instanceof Boolean) 
// Also note that i have to cast the object to a Boolean 
// and then use .booleanValue to get the boolean 
    ed.putBoolean(key, ((Boolean)v).booleanValue()); 
else if(v instanceof Float) 
    ed.putFloat(key, ((Float)v).floatValue()); 
else if(v instanceof Integer) 
    ed.putInt(key, ((Integer)v).intValue()); 
else if(v instanceof Long) 
    ed.putLong(key, ((Long)v).longValue()); 
else if(v instanceof String) 
    ed.putString(key, ((String)v));   
} 
ed.commit(); //save it. 

希望這有助於。

+0

謝謝!我會盡快嘗試這個 – cerealspiller

+3

如果有幫助,請不要忘記接受和/或提出答案; – zarthross

+1

這應該被接受爲答案。您可能還需要添加:\t \t/*用戶設置中設置活動變得持久*/ \t \t如果(SystemUtils.getSDKVersion()> Build.VERSION_CODES.FROYO){ \t \t \t editor.apply(); \t \t} else { \t \t \t editor.commit(); \t \t} –

7

這裏的一個版本也支持字符串集。

public static void copySharedPreferences(SharedPreferences fromPreferences, SharedPreferences toPreferences) { 
    copySharedPreferences(fromPreferences, toPreferences, true); 
} 

public static void copySharedPreferences(SharedPreferences fromPreferences, SharedPreferences toPreferences, boolean clear) { 

    SharedPreferences.Editor editor = toPreferences.edit(); 
    if (clear) { 
     editor.clear(); 
    } 
    copySharedPreferences(fromPreferences, editor); 
    editor.commit(); 
} 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
@SuppressWarnings({"unchecked", "ConstantConditions"}) 
public static void copySharedPreferences(SharedPreferences fromPreferences, SharedPreferences.Editor toEditor) { 

    for (Map.Entry<String, ?> entry : fromPreferences.getAll().entrySet()) { 
     Object value = entry.getValue(); 
     String key = entry.getKey(); 
     if (value instanceof String) { 
      toEditor.putString(key, ((String) value)); 
     } else if (value instanceof Set) { 
      toEditor.putStringSet(key, (Set<String>) value); // EditorImpl.putStringSet already creates a copy of the set 
     } else if (value instanceof Integer) { 
      toEditor.putInt(key, (Integer) value); 
     } else if (value instanceof Long) { 
      toEditor.putLong(key, (Long) value); 
     } else if (value instanceof Float) { 
      toEditor.putFloat(key, (Float) value); 
     } else if (value instanceof Boolean) { 
      toEditor.putBoolean(key, (Boolean) value); 
     } 
    } 
}