SpUtil.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package me.yoqi.android.xunfeitts.utils;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. public class SpUtil {
  5. private static final String SP_NAME = "config";
  6. private static SharedPreferences sp;
  7. public static void saveBoolean(Context context, String key, boolean value) {
  8. if (sp == null) {
  9. sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  10. }
  11. sp.edit().putBoolean(key, value).apply();
  12. }
  13. public static boolean getBoolean(Context context, String key,
  14. boolean defValue) {
  15. if (sp == null) {
  16. sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  17. }
  18. return sp.getBoolean(key, defValue);
  19. }
  20. public static void saveInt(Context context, String key, int value) {
  21. if (sp == null) {
  22. sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  23. }
  24. sp.edit().putInt(key, value).apply();
  25. }
  26. public static int getInt(Context context, String key, int defValue) {
  27. if (sp == null) {
  28. sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  29. }
  30. return sp.getInt(key, defValue);
  31. }
  32. public static String getString(Context context, String key, String defValue) {
  33. if (sp == null) {
  34. sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  35. }
  36. return sp.getString(key,defValue);
  37. }
  38. public static void saveString(Context context, String key, String value) {
  39. if (sp == null) {
  40. sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  41. }
  42. sp.edit().putString(key, value).apply();
  43. }
  44. }