安卓简单缓存类(单例模式)

来源:互联网 发布:微信刷票软件开发 编辑:程序博客网 时间:2024/05/04 06:15
package com.example.cache;import android.content.Context;import android.content.SharedPreferences;/** * Created by Administrator on 2016/3/6. */public class JsonCache {    private static JsonCache jsonCache = null;    public static final String NAME = "com.crazy.gemi.ui.utils.JsonCache";    private SharedPreferences preferences;    private static Context context;    private JsonCache(Context context){        this.context = context;        preferences = context.getSharedPreferences(NAME, 0);    }    public static JsonCache getInstance(Context context){        if (jsonCache == null) {            jsonCache = new JsonCache(context);        }        return jsonCache;    }    public void setJsonCache(String key, String value){        preferences.edit().putString(key, value).commit();    }    public String getJsonCache(String key){        return preferences.getString(key, null);    }    public void clearCache() {        preferences.edit().clear().commit();    }    public void removeCache(String key){        preferences.edit().remove(key).commit();    }}

0 0
原创粉丝点击