下面列出了android.content.SharedPreferences.Editor#putStringSet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 存放object
*
* @param context
* @param fileName
* @param key
* @param object
* @return
*/
public static boolean put(Context context, String fileName, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = sp.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, ((Integer) object).intValue());
} else if (object instanceof Boolean) {
editor.putBoolean(key, ((Boolean) object).booleanValue());
} else if (object instanceof Float) {
editor.putFloat(key, ((Float) object).floatValue());
} else if (object instanceof Long) {
editor.putLong(key, ((Long) object).longValue());
} else if (object instanceof Set) {
editor.putStringSet(key, (Set<String>) object);
} else {
editor.putStringSet(key, (Set<String>) object);
}
return editor.commit();
}
/**
* 存放object
*
* @param context
* @param fileName
* @param key
* @param object
* @return
*/
public static boolean put(Context context, String fileName, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = sp.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, ((Integer) object).intValue());
} else if (object instanceof Boolean) {
editor.putBoolean(key, ((Boolean) object).booleanValue());
} else if (object instanceof Float) {
editor.putFloat(key, ((Float) object).floatValue());
} else if (object instanceof Long) {
editor.putLong(key, ((Long) object).longValue());
} else if (object instanceof Set) {
editor.putStringSet(key, (Set<String>) object);
} else {
editor.putStringSet(key, (Set<String>) object);
}
return editor.commit();
}
@SuppressLint("NewApi")
private void editorPutValues(Editor editor, String preferKey, Object valueData) {
if (editor == null) {
return;
}
if (valueData == null) {
editor.putString(preferKey, null);
} else {
if (valueData instanceof String) {
editor.putString(preferKey, (String) valueData);
} else if (valueData instanceof Integer) {
editor.putInt(preferKey, (Integer) valueData);
} else if (valueData instanceof Boolean) {
editor.putBoolean(preferKey, (Boolean) valueData);
} else if (valueData instanceof Float) {
editor.putFloat(preferKey, (Float) valueData);
} else if (valueData instanceof Set) {
if (Util.isCompateApi(11))
editor.putStringSet(preferKey, (Set<String>) valueData);
} else if (valueData instanceof Long) {
editor.putLong(preferKey, (Long) valueData);
}
}
}
void loadSharedPreferences(SharedPreferences preferences)
{
Editor editor = preferences.edit();
editor.putBoolean(PREF_EVENT_ORIENTATION_ENABLED, _enabled);
String[] splits = this._display.split("\\|");
Set<String> set = new HashSet<>(Arrays.asList(splits));
editor.putStringSet(PREF_EVENT_ORIENTATION_DISPLAY, set);
splits = this._sides.split("\\|");
set = new HashSet<>(Arrays.asList(splits));
editor.putStringSet(PREF_EVENT_ORIENTATION_SIDES, set);
editor.putString(PREF_EVENT_ORIENTATION_DISTANCE, String.valueOf(this._distance));
editor.putBoolean(PREF_EVENT_ORIENTATION_CHECK_LIGHT, this._checkLight);
editor.putString(PREF_EVENT_ORIENTATION_LIGHT_MIN, this._lightMin);
editor.putString(PREF_EVENT_ORIENTATION_LIGHT_MAX, this._lightMax);
editor.putString(PREF_EVENT_ORIENTATION_IGNORED_APPLICATIONS, this._ignoredApplications);
editor.apply();
}
void loadSharedPreferences(SharedPreferences preferences)
{
Editor editor = preferences.edit();
editor.putBoolean(PREF_EVENT_BATTERY_ENABLED, _enabled);
editor.putString(PREF_EVENT_BATTERY_LEVEL_LOW, String.valueOf(this._levelLow));
editor.putString(PREF_EVENT_BATTERY_LEVEL_HIGHT, String.valueOf(this._levelHight));
//editor.putBoolean(PREF_EVENT_BATTERY_CHARGING, this._charging);
editor.putString(PREF_EVENT_BATTERY_CHARGING, String.valueOf(this._charging));
String[] splits;
if (this._plugged != null)
splits = this._plugged.split("\\|");
else
splits = new String[]{};
Set<String> set = new HashSet<>(Arrays.asList(splits));
editor.putStringSet(PREF_EVENT_BATTERY_PLUGGED, set);
editor.putBoolean(PREF_EVENT_BATTERY_POWER_SAVE_MODE, this._powerSaveMode);
editor.apply();
}
/**
* Remove the database from the saved preferences
*
* @param database
* @param preserveOverlays
*/
private void removeDatabaseFromPreferences(String database, boolean preserveOverlays) {
Editor editor = settings.edit();
Set<String> databases = settings.getStringSet(databasePreference,
new HashSet<String>());
if (databases != null && databases.contains(database)) {
Set<String> newDatabases = new HashSet<String>();
newDatabases.addAll(databases);
newDatabases.remove(database);
editor.putStringSet(databasePreference, newDatabases);
}
editor.remove(getTileTablesPreferenceKey(database));
editor.remove(getFeatureTablesPreferenceKey(database));
if (!preserveOverlays) {
editor.remove(getFeatureOverlayTablesPreferenceKey(database));
deleteTableFiles(database);
}
editor.commit();
}
public void saveProfileList(Context context) {
SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
// For reasing I do not understand at all
// Android saves my prefs file only one time
// if I remove the debug code below :(
int counter = sharedprefs.getInt("counter", 0);
editor.putInt("counter", counter + 1);
editor.apply();
Log.d("TEST", "saveProfileList: ------------------- "+counter);
}
public void saveProfileList(Context context) {
SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
// For reasing I do not understand at all
// Android saves my prefs file only one time
// if I remove the debug code below :(
int counter = sharedprefs.getInt("counter", 0);
editor.putInt("counter", counter + 1);
editor.apply();
}
void putStringSet(String key, Set<String> value) {
if (mPref != null) {
Editor editor = mPref.edit();
editor.putStringSet(key, value);
editor.apply();
} else throw new RuntimeException("First Initialize context");
}
/**
* convenience method for setting the given value to this {@link SettingsObject}
* and also saving the value in the apps' settings.<br><br/>
* VERY IMPORTANT!!! <br><br/>
* this method does NOT perform validity checks on the given "value" and
* assumes that it is a valid value to be saved as a setting!
* for example, if this method is called from {@link SeekBarSettingsObject},
* it is assumed that "value" is between {@link SeekBarSettingsObject#getMinValue()}
* and {@link SeekBarSettingsObject#getMaxValue()}
* @param context the context to be used to get the app settings
* @param value the VALID value to be saved in the apps' settings
* @throws IllegalArgumentException if the given value is of a type which cannot be saved
* to SharedPreferences
*/
public void setValueAndSaveSetting(Context context, V value)
throws IllegalArgumentException
{
setValue(value);
Editor editor = EasySettings.retrieveSettingsSharedPrefs(context).edit();
switch (getType())
{
case VOID:
//no actual value to save
break;
case BOOLEAN:
editor.putBoolean(getKey(), (Boolean) value);
break;
case FLOAT:
editor.putFloat(getKey(), (Float) value);
break;
case INTEGER:
editor.putInt(getKey(), (Integer) value);
break;
case LONG:
editor.putLong(getKey(), (Long) value);
break;
case STRING:
editor.putString(getKey(), (String) value);
break;
case STRING_SET:
editor.putStringSet(getKey(), getStringSetToSave((Set<?>) value));
break;
default:
throw new IllegalArgumentException("parameter \"value\" must be of a type that " +
"can be saved in SharedPreferences. given type was "
+ value.getClass().getName());
}
editor.apply();
}
/**
* @param key The name of the preference to modify.
* @param value The new value for the preference.
* @see Editor#putStringSet(String, Set)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void putStringSet(final String key, final Set<String> value) {
final Editor editor = getPreferences().edit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
editor.putStringSet(key, value);
} else {
// Workaround for pre-HC's lack of StringSets
int stringSetLength = 0;
if (mPrefs.contains(key + LENGTH)) {
// First read what the value was
stringSetLength = mPrefs.getInt(key + LENGTH, -1);
}
editor.putInt(key + LENGTH, value.size());
int i = 0;
for (String aValue : value) {
editor.putString(key + "[" + i + "]", aValue);
i++;
}
for (; i < stringSetLength; i++) {
// Remove any remaining values
editor.remove(key + "[" + i + "]");
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
}
public void saveProfileList(Context context) {
SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
// For reasing I do not understand at all
// Android saves my prefs file only one time
// if I remove the debug code below :(
int counter = sharedprefs.getInt("counter", 0);
editor.putInt("counter", counter + 1);
editor.apply();
}
public void saveProfileList(Context context) {
SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
// For reasing I do not understand at all
// Android saves my prefs file only one time
// if I remove the debug code below :(
int counter = sharedprefs.getInt("counter", 0);
editor.putInt("counter", counter + 1);
editor.apply();
}
/**
* 存储一条数据到首选项文件中
* @param preferFileName 要保存的SharedPreference 文件名
* @param key
* @param value
*/
@SuppressLint("NewApi")
public boolean saveValue(String preferFileName, String key, Object value) {
if (Util.isEmpty(preferFileName)) {
return false;
}
Editor mEditor = getSpByName(preferFileName).edit();
if (value == null) {
mEditor.putString(key, null);
}
else{
String valueType = value.getClass().getSimpleName();
if ("String".equals(valueType)) {
mEditor.putString(key, (String) value);
} else if ("Integer".equals(valueType)) {
mEditor.putInt(key, (Integer) value);
} else if ("Boolean".equals(valueType)) {
mEditor.putBoolean(key, (Boolean) value);
} else if ("Float".equals(valueType)) {
mEditor.putFloat(key, (Float) value);
} else if ("Long".equals(valueType)) {
mEditor.putLong(key, (Long) value);
} else if ("Set".equals(valueType)) {
if (Util.isCompateApi(11)) {
mEditor.putStringSet(key, (Set<String>) value);
}
}
}
return mEditor.commit();
}
/**
* Persists a specific set in the shared preferences by using the preference's key.
*
* @param set
* The set, which should be persisted, as an instance of the type {@link Set}
* @return True, if the given set has been persisted, false otherwise
*/
private boolean persistSet(@Nullable final Set<String> set) {
if (set != null && shouldPersist()) {
if (set.equals(getPersistedSet(null))) {
return true;
}
Editor editor = getPreferenceManager().getSharedPreferences().edit();
editor.putStringSet(getKey(), set);
editor.apply();
return true;
}
return false;
}
public void saveProfileList(Context context) {
SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
// For reasing I do not understand at all
// Android saves my prefs file only one time
// if I remove the debug code below :(
int counter = sharedprefs.getInt("counter", 0);
editor.putInt("counter", counter + 1);
editor.apply();
}
/**
* Remove all databases from the preferences file
*/
private void removeAllDatabasesFromPreferences(){
Editor editor = settings.edit();
Set<String> emptyDatabases = new HashSet<String>();
editor.putStringSet(databasePreference, emptyDatabases);
editor.commit();
}
/**
* 批量存储数据 参数的长度一定要一致 并且keys中的key 与 values中同序号的元素要一一对应,以保证数据存储的正确性
* @param preferFileName
* @param keys
* @param vTypes keys中键key要存储的对应的 values中的元素的数据类型 eg.: keys[0]= "name"; vTypes[0] = V_TYPE_STR;values[0]="ZhangSan"
* @param values 要存储的值
*/
@Deprecated
@SuppressLint("NewApi")
public boolean saveValue(String preferFileName, String[] keys, byte[] vTypes, Object... values) {
if (keys == null || vTypes == null || values == null) {
return false;
}
int keysLen = keys.length;
if (keysLen == 0)
return false;
int vTypesLen = vTypes.length;
int valuesLen = values.length;
if (vTypesLen == 0 || valuesLen == 0) {
return false;
}
//三者长度取最小的,以保证任何一个数组中不出现异常
int canOptLen = Math.min(keysLen, Math.min(vTypesLen,valuesLen));
Editor mEditor = getSpByName(preferFileName).edit();
for (int i = 0; i < canOptLen; i++) {
byte vType = vTypes[i];
String key = keys[i];
Object v = values[i];
switch (vType) {
case V_TYPE_STR:
mEditor.putString(key, (String) v);
break;
case V_TYPE_INT:
mEditor.putInt(key, (Integer) v);
break;
case V_TYPE_BOOL:
mEditor.putBoolean(key, (Boolean) v);
break;
case V_TYPE_LONG:
mEditor.putLong(key, (Long) v);
break;
case V_TYPE_FLOAT:
mEditor.putFloat(key, (Float) v);
break;
case V_TYPE_SET_STR:
if (Util.isCompateApi(11)) {
mEditor.putStringSet(key, (Set<String>) v);
}
break;
}
}
return mEditor.commit();
}
private void saveExtAppList(Set<String> allowedapps) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Editor prefedit = prefs.edit();
prefedit.putStringSet(PREFERENCES_KEY, allowedapps);
prefedit.apply();
}
private void save(@NonNull String key, @NonNull Set<String> values) {
Editor editor = getSharedPreferences().edit();
editor.putStringSet(key, values);
editor.apply();
}