首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:复制/复制SharedPreferences

Android:复制/复制SharedPreferences
EN

Stack Overflow用户
提问于 2011-09-21 07:36:24
回答 3查看 5.3K关注 0票数 14

有没有办法复制或复制SharedPreference?或者我需要从一个变量中获取每个变量,然后将它们放入另一个变量中?

EN

回答 3

Stack Overflow用户

发布于 2011-10-06 04:11:16

尝试如下所示:

代码语言:javascript
复制
//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.

希望这能有所帮助。

票数 16
EN

Stack Overflow用户

发布于 2018-09-28 07:36:42

Kotlin版本:

代码语言:javascript
复制
fun SharedPreferences.copyTo(dest: SharedPreferences) = with(dest.edit()) {
  for (entry in all.entries) {
    val value = entry.value ?: continue
    val key = entry.key
    when (value) {
      is String -> putString(key, value)
      is Set<*> -> putStringSet(key, value as Set<String>)
      is Int -> putInt(key, value)
      is Long -> putLong(key, value)
      is Float -> putFloat(key, value)
      is Boolean -> putBoolean(key, value)
      else -> error("Unknown value type: $value")
    }
  }
  apply()
}

用法:

代码语言:javascript
复制
val prefs1 = getSharedPreferences("test1", MODE_PRIVATE)
val prefs2 = getSharedPreferences("test2", MODE_PRIVATE)

prefs1.copyTo(prefs2)
票数 10
EN

Stack Overflow用户

发布于 2014-05-03 02:01:03

这里是一个也支持字符串集的版本。

代码语言:javascript
复制
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);
        }
    }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7493029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档