SharedPreferences存储 单例模式

public class SharedUtils {
   private static SharedUtils sharedUtils;
   public static String SHARED_PREFS_NAME = Constants.APPNAME_ENGLISH;

   public static SharedUtils singleton() {
      if (sharedUtils == null) {
         sharedUtils = new SharedUtils(UIUtil.getContext(), SHARED_PREFS_NAME);
      }
      return sharedUtils;
   }
   
   private SharedPreferences mSharedPrefs;

   private SharedUtils(Context context, String sharedPrefsName) {
      mSharedPrefs = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE);
   }

   public <T> void put(String key, T value) {
      put(new String[] { key }, new Object[] { value });
   }

   public <T> void put(String[] keys, T[] values) {
      if (values != null && keys != null) {
         Editor edit = mSharedPrefs.edit();
         for (int i = 0; i < values.length; i++) {
            T value = values[i];
            int index = i;
            if (index >= keys.length) {
               index = keys.length - 1;
            }
            String key = keys[index];
            Class<? extends Object> cls = value.getClass();
            if (cls == Integer.class || cls == int.class) {
               edit.putInt(key, (Integer) value);
            } else if (cls == Boolean.class || cls == boolean.class) {
               edit.putBoolean(key, (Boolean) value);
            } else if (cls == Float.class || cls == float.class) {
               edit.putFloat(key, (Float) value);
            } else if (cls == Long.class || cls == long.class) {
               edit.putLong(key, (Long) value);
            } else if (cls == String.class) {
               edit.putString(key, (String) value);
            }
         }
         edit.commit();
      }
   }

   @SuppressWarnings("unchecked")
   public <T> T get(String key, T defValue) {
      Object result = null;
      if (defValue != null && key != null) {
         Class<? extends Object> cls = defValue.getClass();
         if (cls == Integer.class || cls == int.class) {
            result = mSharedPrefs.getInt(key, (Integer) defValue);
         } else if (cls == Boolean.class || cls == boolean.class) {
            result = mSharedPrefs.getBoolean(key, (Boolean) defValue);
         } else if (cls == Float.class || cls == float.class) {
            result = mSharedPrefs.getFloat(key, (Float) defValue);
         } else if (cls == Long.class || cls == long.class) {
            result = mSharedPrefs.getLong(key, (Long) defValue);
         } else if (cls == String.class) {
            result = mSharedPrefs.getString(key, (String) defValue);
         }
      }
      return (T) result;
   }

   public void clear() {
      Editor edit = mSharedPrefs.edit();
      edit.clear();
      edit.commit();
   }

   public void clear(String key) {
      Editor edit = mSharedPrefs.edit();
      edit.remove(key);
      edit.commit();
   }

}

存储方式:

SharedUtils sharedUtils = SharedUtils.singleton();
sharedUtils.put(String key, T value);

获取存储方式:
SharedUtils sharedUtils = SharedUtils.singleton();
sharedUtils.get(String key, T value);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值