public static Set<String> getValue(Context context, String file, String key,
Set<String> defValue){
try {
if (context == null) {
return defValue;
}
SharedPreferences sp = context.getSharedPreferences(file,
Context.MODE_PRIVATE);
if (Build.VERSION.SDK_INT < 11) {
final String regularEx = "|";
String setString = sp.getString(key, null);
if (setString != null){
String[] values = setString.split(regularEx);
Set<String> set = new HashSet<String>(values.length);
for (String value : values) {
if (!TextUtils.isEmpty(value))
set.add(value);
}
return set;
}
} else {
return sp.getStringSet(key, defValue);
}
} catch (Throwable e) {
if (AppDebugConfig.IS_DEBUG) {
Debug_AdLog.e(e);
}
}
return defValue;
}
public static void putValue(Context context, String file, String key,
Set<String> values) {
try {
if (context == null) {
return;
}
SharedPreferences sp = context.getSharedPreferences(file,
Context.MODE_PRIVATE);
if (Build.VERSION.SDK_INT < 11) {
final String regularEx = "|";
String str = "";
if (values != null && !values.isEmpty()) {
Object[] objects = values.toArray();
for (Object obj : objects) {
str += obj.toString();
str += regularEx;
}
sp.edit().putString(key, str);
}
} else {
sp.edit().putStringSet(key, values).commit();
}
} catch (Throwable e) {
if (AppDebugConfig.IS_DEBUG) {
Debug_AdLog.e(e);
}
}
}
sdk 11以下兼容SharedPreferences的getStringset方法
最新推荐文章于 2022-04-05 15:37:32 发布