SharedPreferencesUtils.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.ynstkz.shitu.android.data;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.content.pm.PackageManager;
  5. import com.common.library.utils.AppInfoUtil;
  6. import com.ynstkz.shitu.android.application.STApplication;
  7. import com.ynstkz.shitu.android.common.Constants;
  8. /**
  9. * Created by fucl on 2016/4/6.
  10. */
  11. public class SharedPreferencesUtils {
  12. private static final String FILE_NAME = Constants.ST_SHAREPREFREENCE;
  13. private static final String PACK_NAME = AppInfoUtil.getAppPackageName(STApplication.getContext());
  14. /**
  15. * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
  16. * @param key 键名
  17. * @param object 值
  18. */
  19. public static boolean set(String key, Object object) {
  20. try {
  21. Context otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
  22. String type = object.getClass().getSimpleName();
  23. SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
  24. SharedPreferences.Editor editor = sp.edit();
  25. if ("String".equals(type)) {
  26. editor.putString(key, (String) object);
  27. } else if ("Integer".equals(type)) {
  28. editor.putInt(key, (Integer) object);
  29. } else if ("Boolean".equals(type)) {
  30. editor.putBoolean(key, (Boolean) object);
  31. } else if ("Float".equals(type)) {
  32. editor.putFloat(key, (Float) object);
  33. } else if ("Long".equals(type)) {
  34. editor.putLong(key, (Long) object);
  35. }
  36. return editor.commit();
  37. } catch (PackageManager.NameNotFoundException e) {
  38. e.printStackTrace();
  39. }
  40. return false;
  41. }
  42. public static boolean remove(String key){
  43. Context otherAppsContext;
  44. try {
  45. otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
  46. SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
  47. SharedPreferences.Editor editor = sp.edit();
  48. editor.remove(key);
  49. return editor.commit();
  50. } catch (PackageManager.NameNotFoundException e) {
  51. e.printStackTrace();
  52. return false;
  53. }
  54. }
  55. /**
  56. * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
  57. * @param key
  58. * @param defaultObject
  59. * @return
  60. */
  61. public static Object get(String key, Object defaultObject) {
  62. try {
  63. String type = defaultObject.getClass().getSimpleName();
  64. Context otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
  65. SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
  66. if ("String".equals(type)) {
  67. return sp.getString(key, (String) defaultObject);
  68. } else if ("Integer".equals(type)) {
  69. return sp.getInt(key, (Integer) defaultObject);
  70. } else if ("Boolean".equals(type)) {
  71. return sp.getBoolean(key, (Boolean) defaultObject);
  72. } else if ("Float".equals(type)) {
  73. return sp.getFloat(key, (Float) defaultObject);
  74. } else if ("Long".equals(type)) {
  75. return sp.getLong(key, (Long) defaultObject);
  76. }
  77. return defaultObject;
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. return defaultObject;
  81. }
  82. }
  83. }