赞
踩
package com.coral3.common_module.utils; import android.app.Application; import android.content.SharedPreferences; /** * @author 蓝之静云 * @description 本地存储工具类 * @date 2021-12-* */ public class SharedPrefUtils { private static SharedPrefUtils instance; private SharedPreferences sp; private SharedPrefUtils(Application application) { sp = application.getSharedPreferences( application.getPackageName(), 0); } public static SharedPrefUtils getInstance() { if (instance == null) { synchronized (SharedPrefUtils.class) { if (instance == null) { instance = new SharedPrefUtils((Application) InitUtil.getContext().getApplicationContext()); } } } return instance; } public void put(String key, String value) { sp.edit().putString(key, value).commit(); } public String get(String key, String defValue) { return sp.getString(key, defValue); } public void put(String key, Integer value) { sp.edit().putInt(key, value).commit(); } public Integer get(String key, Integer defValue) { return sp.getInt(key, defValue); } public void put(String key, Float value) { sp.edit().putFloat(key, value).commit(); } public Float get(String key, Float defValue) { return sp.getFloat(key, defValue); } public void put(String key, Long value) { sp.edit().putLong(key, value).commit(); } public Long get(String key, Long defValue) { return sp.getLong(key, defValue); } public void put(String key, boolean value) { sp.edit().putBoolean(key, value).commit(); } public Boolean get(String key, boolean defValue) { return sp.getBoolean(key, defValue); } public void remove(String key) { try { sp.edit().remove(key).commit(); } catch (Exception e) { e.printStackTrace(); } } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。