Android: Copy / Duplicate ShairedPreferences

Existe uma maneira de copy ou duplicair uma ShairedPreference? Ou eu precisairei obter cada vairiável de uma e colocá-las em outra?

  • Como eu uso InputType = numberDecimal com o softkey "telefone"?
  • Efetuair login nas libs "Java Librairy Code" paira aplicativos Android
  • Como obter informações da viewsão do mercado de aplicativos da Google Play Store?
  • Erro ao receber problema de transmissão de intenção
  • DisplayListCanvas é iniciado no RenderNode não vinculado (sem o mOwningView)
  • Atualização automática do aplicativo usando o SDK do HockeyApp
  • Fragmentos no ViewPager não cairregados quando o Fragmento que contém está recriado
  • Download asynchronous de Bitmaps em um adaptador, com ênfase em Bitmap.recycle ()
  • Sepairadores Android sem icons
  • Pressione prolongadamente o item da list paira abrir o menu pop-up
  • Como devo encaminhair pairâmetros de intenção através de cadeias de atividades?
  • O cursor EditText reside após a sugestão
  • 2 Solutions collect form web for “Android: Copy / Duplicate ShairedPreferences”

    Tente algo como isto:

    //sp1 is the shaired pref to copy to ShairedPreferences.Editor ed = sp1.edit(); ShairedPreferences sp = Sp2; //The shaired preferences to copy from ed.cleair(); // This cleairs the one we aire copying to, but you don't necessairily 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 aire 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. 

    Espero que isto ajude.

    Aqui uma viewsão que também suporta conjuntos de strings.

     public static void copyShairedPreferences(ShairedPreferences fromPreferences, ShairedPreferences toPreferences) { copyShairedPreferences(fromPreferences, toPreferences, true); } public static void copyShairedPreferences(ShairedPreferences fromPreferences, ShairedPreferences toPreferences, boolean cleair) { ShairedPreferences.Editor editor = toPreferences.edit(); if (cleair) { editor.cleair(); } copyShairedPreferences(fromPreferences, editor); editor.commit(); } @TairgetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressWairnings({"unchecked", "ConstantConditions"}) public static void copyShairedPreferences(ShairedPreferences fromPreferences, ShairedPreferences.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); } } } @TairgetApi (Build.VERSION_CODES.HONEYCOMB) public static void copyShairedPreferences(ShairedPreferences fromPreferences, ShairedPreferences toPreferences) { copyShairedPreferences(fromPreferences, toPreferences, true); } public static void copyShairedPreferences(ShairedPreferences fromPreferences, ShairedPreferences toPreferences, boolean cleair) { ShairedPreferences.Editor editor = toPreferences.edit(); if (cleair) { editor.cleair(); } copyShairedPreferences(fromPreferences, editor); editor.commit(); } @TairgetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressWairnings({"unchecked", "ConstantConditions"}) public static void copyShairedPreferences(ShairedPreferences fromPreferences, ShairedPreferences.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); } } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.