123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package com.ynstkz.shitu.android.data;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import com.common.library.utils.AppInfoUtil;
- import com.ynstkz.shitu.android.application.STApplication;
- import com.ynstkz.shitu.android.common.Constants;
- /**
- * Created by fucl on 2016/4/6.
- */
- public class SharedPreferencesUtils {
- private static final String FILE_NAME = Constants.ST_SHAREPREFREENCE;
- private static final String PACK_NAME = AppInfoUtil.getAppPackageName(STApplication.getContext());
- /**
- * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
- * @param key 键名
- * @param object 值
- */
- public static boolean set(String key, Object object) {
- try {
- Context otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
- String type = object.getClass().getSimpleName();
- SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
- SharedPreferences.Editor editor = sp.edit();
- if ("String".equals(type)) {
- editor.putString(key, (String) object);
- } else if ("Integer".equals(type)) {
- editor.putInt(key, (Integer) object);
- } else if ("Boolean".equals(type)) {
- editor.putBoolean(key, (Boolean) object);
- } else if ("Float".equals(type)) {
- editor.putFloat(key, (Float) object);
- } else if ("Long".equals(type)) {
- editor.putLong(key, (Long) object);
- }
- return editor.commit();
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return false;
- }
- public static boolean remove(String key){
- Context otherAppsContext;
- try {
- otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
- SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
- SharedPreferences.Editor editor = sp.edit();
- editor.remove(key);
- return editor.commit();
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
- * @param key
- * @param defaultObject
- * @return
- */
- public static Object get(String key, Object defaultObject) {
- try {
- String type = defaultObject.getClass().getSimpleName();
- Context otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
- SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
- if ("String".equals(type)) {
- return sp.getString(key, (String) defaultObject);
- } else if ("Integer".equals(type)) {
- return sp.getInt(key, (Integer) defaultObject);
- } else if ("Boolean".equals(type)) {
- return sp.getBoolean(key, (Boolean) defaultObject);
- } else if ("Float".equals(type)) {
- return sp.getFloat(key, (Float) defaultObject);
- } else if ("Long".equals(type)) {
- return sp.getLong(key, (Long) defaultObject);
- }
- return defaultObject;
- } catch (Exception e) {
- e.printStackTrace();
- return defaultObject;
- }
- }
- }
|