2011-09-20 33 views
7

Có cách nào để sao chép hoặc sao chép SharedPreference không? Hoặc tôi sẽ cần phải nhận được mỗi biến từ một biến và sau đó đặt chúng vào một biến khác?Android: Copy/Duplicate SharedPreferences

+0

nhận từng biến từ một và đặt chúng vào một biến khác chỉ là cách tôi sắp xếp. Nhưng SharedPrefs được lưu trữ dưới dạng tệp xml, tôi tưởng tượng bạn có thể sao chép toàn bộ tệp đó và dán nó bằng tên mới bằng cách nào đó. Cách tiếp cận đó có thể yêu cầu một thiết bị bắt nguồn từ để có thể nhận các luồng đầu vào và đầu ra được thiết lập cho thư mục SharedPreferences của ứng dụng của bạn. – FoamyGuy

+0

tại sao bạn muốn sao chép các prefs được chia sẻ? Giải thích chi tiết hơn một chút về những gì bạn đang cố gắng đạt được và nó sẽ giúp chúng tôi cung cấp một câu trả lời phù hợp. – Kenny

+0

Ứng dụng của tôi lưu trữ các biến của nó trong sharedpreference. Tôi có khoảng 50 biến liên tục thay đổi, nói cách khác Chúng không thể được mã hóa cứng trong ứng dụng. Tôi muốn có thể đặt các biến này sang một bên để người dùng ứng dụng có thể bắt đầu một phiên mới và sau đó thay thế giữa hai phiên. Tôi cho rằng tôi có thể hút nó lên và viết ra tất cả các biến cho sharedpreference khác, nhưng nó sẽ dễ dàng hơn nhiều nếu tôi chỉ có thể làm điều này: savedSharedPreference = sharedPreference. LoL – cerealspiller

Trả lời

12

Hãy thử như thế này:

//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. 

Hope this helps.

+0

Cảm ơn! Tôi sẽ thử ASAP – cerealspiller

+3

này đừng quên chấp nhận và/hoặc upvote câu trả lời nếu nó giúp;) – zarthross

+1

điều này sẽ được chấp nhận làm câu trả lời. Ngoài ra, bạn có thể muốn thêm: \t \t/* Cài đặt người dùng được thực hiện liên tục trong Hoạt động Cài đặt */ \t \t if (SystemUtils.getSDKVersion()> Build.VERSION_CODES.FROYO) { \t \t \t editor.apply(); \t \t} else { \t \t \t editor.commit(); \t \t} –

7

Đây là phiên bản cũng hỗ trợ các bộ chuỗi.

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); 
     } 
    } 
} 
Các vấn đề liên quan