import android.content.Context; import android.content.SharedPreferences; /** * Created by wzc on 2016/3/11. */ /** * 进行sp操作的工具类 */ public class SpUtils { private static SpUtils instance; public static SpUtils getInstance(Context context) { if (instance == null) { sp = context.getSharedPreferences("xreal", Context.MODE_PRIVATE); instance = new SpUtils(); } return instance; } /** * 保存数据 * * @param name * @param value */ public void save(String name, Object value) { if (value instanceof String) { sp.edit().putString(name, (String) value).apply(); } else if (value instanceof Integer) { sp.edit().putInt(name, (Integer) value).apply(); } else if (value instanceof Boolean) { sp.edit().putBoolean(name, (Boolean) value).apply(); } } public int getInt(String name, int defValue) { return sp.getInt(name, defValue); } public String getString(String name, String defValue) { return sp.getString(name, defValue); } public boolean getBoolean(String name, Boolean defValue) { return sp.getBoolean(name, defValue); } }
spUtils
最新推荐文章于 2024-04-22 13:03:51 发布