android 缓存工具类 SharePreferenceUtil

各种数据类型的put和get都已经实现,且实现了缓存的clear和remove方法。

直接上码:

public class SharePreferenceUtil {
    
    private static final String TAG = SharePreferenceUtil.class.getSimpleName();
    private static final String DEFAULT_SHARE_NAME = "biliparser";
    private static SharedPreferences sharedPreferences;
    
    public static void init(Context context){
        sharedPreferences = context.getSharedPreferences(DEFAULT_SHARE_NAME,Context.MODE_PRIVATE);
    }
    
    public static void put(String key,String value){
        sharedPreferences.edit().putString(key,value).apply();
    }
    
    public static String get(String key){
        return sharedPreferences.getString(key,"");
    }
    
    public static String getString(String key,String defaultValue){
        return sharedPreferences.getString(key,defaultValue);
    }
    
    public static void put(String key,int value){
        sharedPreferences.edit().putInt(key,value).apply();
    }

    public static int getInt(String key){
        return sharedPreferences.getInt(key,0);
    }

    public static int getInt(String key,int defaultValue){
        return sharedPreferences.getInt(key,defaultValue);
    }
    
    public static void put(String key,Long value){
        sharedPreferences.edit().putLong(key,value).apply();
    }

    public static Long getLong(String key){
        return sharedPreferences.getLong(key,0);
    }

    public static Long getLong(String key,Long defaultValue){
        return sharedPreferences.getLong(key,defaultValue);
    }
    
    public static void put(String key,boolean value){
        sharedPreferences.edit().putBoolean(key,value).apply();
    }

    public static boolean getBoolean(String key){
        return sharedPreferences.getBoolean(key,false);
    }

    public static boolean getBoolean(String key,boolean defaultValue){
        return sharedPreferences.getBoolean(key,defaultValue);
    }
    
    public static void put(String key,float value){
        sharedPreferences.edit().putFloat(key,value).apply();
    }

    public static float getFloat(String key){
        return sharedPreferences.getFloat(key,0);
    }

    public static float getFloat(String key,float defaultValue){
        return sharedPreferences.getFloat(key,defaultValue);
    }
    
    public static boolean contains(String key){
        return sharedPreferences.contains(key);
    }
    
    public static void remove(String key){
        sharedPreferences.edit().remove(key).apply();
    }
    
    public static void clear(){
        sharedPreferences.edit().clear().apply();
    }
    
}

移植到项目中导入相关包即可使用,缓存名字可以自己改,我这里用的是biliparser。

使用前先调用init方法,建议在Application类中调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JesseAndroid

每一份支持都是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值