Settings.Global
public class SetGlobal {
private static SetGlobal sg;
private static Context mContext;
public static final String key = "negativescreen_dataKey";
public static SetGlobal getInstance(Context context) {
mContext = context;
if (sg == null) {
synchronized (SetGlobal.class) {
if (sg == null) {
sg = new SetGlobal();
}
}
}
return sg;
}
public void put(List<Icn> dataList) {
Gson gson = new Gson();
Settings.Global.putString(mContext.getContentResolver(), key, gson.toJson(dataList));
}
public List<Icn> get() {
Gson gson = new Gson();
String updata = Settings.Global.getString(mContext.getContentResolver(), key);
List<Icn> dataList = gson.fromJson(updata, new ParameterizedTypeImpl(List.class, new Class[]{Icn.class}));
if (dataList == null) {
return new ArrayList<>();
}
return dataList;
}
public void setListener(String key, ContentObserver observer) {
ContentResolver contentResolver = mContext.getContentResolver();
Uri uri = Settings.Global.getUriFor(key);
contentResolver.registerContentObserver(uri, false, observer);
}
}
public class ParameterizedTypeImpl implements ParameterizedType {
private final Class raw;
private final Type[] args;
public ParameterizedTypeImpl(Class raw, Type[] args) {
this.raw = raw;
this.args = args != null ? args : new Type[0];
}
@Override
public Type[] getActualTypeArguments() {
return args;
}
@Override
public Type getRawType() {
return raw;
}
@Override
public Type getOwnerType() {
return null;
}
}
本文来自博客园,作者:阿寳同學Zybao,转载请注明原文链接:https://www.cnblogs.com/zybao/p/18604218

浙公网安备 33010602011771号