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;
    }

}
posted @ 2024-12-13 09:46  阿寳同學Zybao  阅读(30)  评论(0)    收藏  举报