क्या SharePreference कॉपी या डुप्लिकेट करने का कोई तरीका है? या क्या मुझे प्रत्येक चर को एक से प्राप्त करने की आवश्यकता होगी और फिर उन्हें दूसरे में रखा जाएगा?एंड्रॉइड: प्रतिलिपि बनाएँ/डुप्लिकेट साझा करें
उत्तर
कोशिश कुछ इस तरह:
//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.
आशा इस मदद करता है।
धन्यवाद! मैं इस ASAP – cerealspiller
को उत्तर देने के लिए उत्तर स्वीकार करने और/या ऊपर उठाने के लिए मत भूलना;) – zarthross
इसे उत्तर के रूप में स्वीकार किया जाना चाहिए। इसके अलावा, आप जोड़ सकते हैं: \t \t/* उपयोगकर्ता सेटिंग सेटिंग गतिविधि में लगातार बना रहे हैं */ \t \t अगर (SystemUtils.getSDKVersion()> Build.VERSION_CODES.FROYO) { \t \t \t editor.apply(); \t \t} अन्य { \t \t \t editor.commit(); \t \t} –
यहां एक संस्करण जो स्ट्रिंग सेट का भी समर्थन करता है।
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);
}
}
}
प्रत्येक चर को एक से प्राप्त करें और उन्हें दूसरे में रखें, केवल एक ही तरीका है जिसे मैं जानता हूं। लेकिन SharedPrefs को एक XML फ़ाइल के रूप में संग्रहीत किया जाता है, मुझे लगता है कि आप उस संपूर्ण फ़ाइल की प्रतिलिपि बनाने में सक्षम हो सकते हैं और किसी भी तरह से इसे किसी नए नाम से पेस्ट कर सकते हैं। उस दृष्टिकोण को रूट डिवाइस की आवश्यकता हो सकती है, हालांकि आपके ऐप्स में इनपुट और आउटपुट स्ट्रीम सेट करने में सक्षम होने के लिए SharePreferences फ़ोल्डर। – FoamyGuy
आप साझा किए गए prefs को कॉपी क्यों करना चाहते हैं? कुछ और विस्तार से बताएं कि यह आपके द्वारा प्राप्त करने की कोशिश कर रहा है और यह हमें एक उपयुक्त उत्तर प्रदान करने में मदद करेगा। – Kenny
मेरा ऐप साझा किए गए संदर्भ में इसके चर को संग्रहीत करता है। मेरे पास लगभग 50 चर हैं जो लगातार बदल रहे हैं, दूसरे शब्दों में उन्हें ऐप में हार्डकोड नहीं किया जा सकता है। मैं इन चरों को एक तरफ सेट करने में सक्षम होना चाहता हूं ताकि ऐप उपयोगकर्ता एक नया सत्र शुरू कर सके और फिर दोनों के बीच वैकल्पिक हो। मुझे लगता है कि मैं इसे चूस सकता हूं और सभी चरों को किसी अन्य साझािकरण में लिख सकता हूं, लेकिन अगर मैं ऐसा कर सकता तो यह इतना आसान होगा: सहेजे गए शेरफेरेंस = साझा प्रेफरेंस। लोएल – cerealspiller