Here you can find the source of putSharedPrefs(Context context, String prefName, int mode, Map
Parameter | Description |
---|---|
context | - Current context |
prefName | - Desired preferences file name |
mode | - Mode to open shared preferences |
pairs | - map to put in shared preferences |
public static void putSharedPrefs(Context context, String prefName, int mode, Map<String, String> pairs)
//package com.java2s; import java.util.Map; import java.util.Map.Entry; import android.content.Context; import android.content.SharedPreferences; public class Main { /**/*from ww w. j a va 2s . com*/ * Purpose - Put shared preferences through the Shared Preferences API. * Use this if you need multiple shared preference files identified by name. * * @param context - Current context * @param prefName - Desired preferences file name * @param mode - Mode to open shared preferences * @param pairs - map to put in shared preferences * */ public static void putSharedPrefs(Context context, String prefName, int mode, Map<String, String> pairs) { SharedPreferences prefs = context.getSharedPreferences(prefName, mode); if (pairs != null && !pairs.isEmpty()) { SharedPreferences.Editor editor = prefs.edit(); for (Entry<String, String> pair : pairs.entrySet()) { editor.putString(pair.getKey(), pair.getValue()).commit(); } editor.commit(); } } }