SharePreference工具类

简介: SharePreference工具类
import com.eyeguard.app.application.EyeGuardApplication;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
 * 主要功能:用于存储缓存数据
 *@author caijin
 */
public class AppSharePreferenceMgr {
//定义sharedPreferences
    private static SharedPreferences sharedPreferences;  
    //定义editor
private static Editor editor;  
    //传输数据类型判断
    private static String CLASS_SIMPLENAME = null;
    /**
     * 清除缓存数据(默认缓存文件名称)
     * @param context 上下文对象
     */
    public static void cleanSharedPreferences(Context context){
    sharedPreferences = context.getSharedPreferences(EyeGuardApplication.APP_SHARE_FILE, Context.MODE_PRIVATE);  
         editor = sharedPreferences.edit();  
         editor.clear();
         editor.commit();
    }
    /**
     * 清除缓存数据(指定缓存文件名称)
     * @param context 上下文对象
     * @param fileKey 缓存文件名称(唯一标识)
     */
    public static void cleanSharedPreferences(Context context, String fileKey){
    sharedPreferences = context.getSharedPreferences(fileKey, Context.MODE_PRIVATE);  
        editor = sharedPreferences.edit();  
        editor.clear();
        editor.commit();
   }
    /** 
     * 不同数据类型缓存设置(默认缓存文件名称)<br/>
     * AppSharePreferenceMgr.setParam(this, "key1", "caijin")<br/>
* AppSharePreferenceMgr.setParam(this, "key2", 10)<br/>
* AppSharePreferenceMgr.setParam(this, "key3", true)<br/>
* AppSharePreferenceMgr.setParam(this, "key4", 100L)<br/>  
* AppSharePreferenceMgr.setParam(this, "key5", 1.1f)<br/>  
     * @param context 上下文对象
     * @param key  设置KEY
     * @param object 设置VALUE
     */  
    public static void setParam(Context context, String key, Object object){  
        sharedPreferences = context.getSharedPreferences(EyeGuardApplication.APP_SHARE_FILE, Context.MODE_PRIVATE);  
        editor = sharedPreferences.edit();  
        CLASS_SIMPLENAME = object.getClass().getSimpleName();
        if("String".equals(CLASS_SIMPLENAME)){  
            editor.putString(key, (String) object);
            editor.commit(); 
            return;
        }  
        if("Integer".equals(CLASS_SIMPLENAME)){  
            editor.putInt(key, (Integer) object);
            editor.commit(); 
            return;
        }  
        if("Boolean".equals(CLASS_SIMPLENAME)){  
            editor.putBoolean(key, (Boolean) object);
            editor.commit(); 
            return;
        }  
        if("Float".equals(CLASS_SIMPLENAME)){  
            editor.putFloat(key, (Float) object);
            editor.commit(); 
            return;
        }  
        if("Long".equals(CLASS_SIMPLENAME)){  
            editor.putLong(key, (Long) object);
            editor.commit(); 
            return;
        }  
    }  
    /**
     * 不同数据类型缓存设置(指定缓存文件名称)<br/>
     * AppSharePreferenceMgr.setParam(this, "userId", "key1", "caijin")<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key2", 10)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key3", true)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key4", 100L)<br/>  
* AppSharePreferenceMgr.setParam(this, "userId", "key5", 1.1f)<br/>  
     * @param context 上下文对象
     * @param fileKey 缓存文件名称(唯一标识)
     * @param key   缓存KEY
     * @param object 缓存VALUE
     */
    public static void setParam(Context context, String fileKey, String key, Object object){  
        sharedPreferences = context.getSharedPreferences(fileKey, Context.MODE_PRIVATE);  
        editor = sharedPreferences.edit();  
        CLASS_SIMPLENAME = object.getClass().getSimpleName();
        if("String".equals(CLASS_SIMPLENAME)){  
            editor.putString(key, (String) object);
            editor.commit(); 
            return;
        }  
        if("Integer".equals(CLASS_SIMPLENAME)){  
            editor.putInt(key, (Integer) object);
            editor.commit(); 
            return;
        }  
        if("Boolean".equals(CLASS_SIMPLENAME)){  
            editor.putBoolean(key, (Boolean) object);
            editor.commit(); 
            return;
        }  
        if("Float".equals(CLASS_SIMPLENAME)){  
            editor.putFloat(key, (Float) object);
            editor.commit(); 
            return;
        }  
        if("Long".equals(CLASS_SIMPLENAME)){  
            editor.putLong(key, (Long) object);
            editor.commit(); 
            return;
        }
    }
    /** 
     * 不同数据类型缓存读取(默认缓存文件名称)<br/>
* AppSharePreferenceMgr.getParam(this, "key1", "caijin")<br/>                                                                                        
* AppSharePreferenceMgr.getParam(this, "key2", 0)<br/>  
* AppSharePreferenceMgr.getParam(this, "key3", false)<br/>  
* AppSharePreferenceMgr.getParam(this, "key4", 0L)<br/>  
* AppSharePreferenceMgr.getParam(this, "key5", 0.0f)<br/>  
     * @param context 上下文对象
     * @param key  获取KEY值
     * @param defValue 默认数据值
     * @return 实际数据值
     */  
    public static Object getParam(Context context, String key, Object defValue){  
        sharedPreferences = context.getSharedPreferences(EyeGuardApplication.APP_SHARE_FILE, Context.MODE_PRIVATE);  
        CLASS_SIMPLENAME = defValue.getClass().getSimpleName();  
        if("String".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getString(key, (String) defValue);
        }  
        if("Integer".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getInt(key, (Integer) defValue);
        }  
        if("Boolean".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        }  
        if("Float".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getFloat(key, (Float) defValue);
        }  
        if("Long".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getLong(key, (Long) defValue);
        }  
        return null;  
    }  
    /** 
     * 不同数据类型缓存设置(指定缓存文件名称)<br/>
     * AppSharePreferenceMgr.setParam(this, "userId", "key1", "caijin")<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key2", 10)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key3", true)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key4", 100L)<br/>  
* AppSharePreferenceMgr.setParam(this, "userId", "key5", 1.1f)<br/>  
     * @param context 上下文对象
     * @param fileKey 缓存文件名称(唯一标识)
     * @param key  设置KEY
     * @param object 设置VALUE
     */  
    public static Object getParam(Context context, String fileKey, String key, Object defValue){  
        sharedPreferences = context.getSharedPreferences(fileKey, Context.MODE_PRIVATE);  
        CLASS_SIMPLENAME = defValue.getClass().getSimpleName();  
        if("String".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getString(key, (String) defValue);
        }  
        if("Integer".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getInt(key, (Integer) defValue);
        }  
        if("Boolean".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        }  
        if("Float".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getFloat(key, (Float) defValue);
        }  
        if("Long".equals(CLASS_SIMPLENAME)){  
            return sharedPreferences.getLong(key, (Long) defValue);
        }  
        return null;  
    } 
}


有代码洁癖的可以使用apply来替换掉commit,apply是异步的,感兴趣的可以了解一下

最后附上所有工具类的下载链接:

http://download.csdn.net/detail/u014727709/9697759



欢迎start,欢迎评论,欢迎指正



相关文章
9hutool实战:LocalDateTimeUtil(JDK8+中的LocalDateTime 工具类封装)
9hutool实战:LocalDateTimeUtil(JDK8+中的LocalDateTime 工具类封装)
735 0
9hutool实战:LocalDateTimeUtil(JDK8+中的LocalDateTime 工具类封装)
|
4月前
|
Android开发
SharePreference封装成工具类
SharePreference封装成工具类
140 1
|
7月前
|
JSON 网络协议 C#
C# 工具类
C# 工具类
50 1
|
7月前
|
Java
JavaMap工具类(MapUtils)
JavaMap工具类(MapUtils)
|
数据采集 算法 安全
一天一个 JUC 工具类 -- 真工具类
CountDownLatch CyclicBarrier ForkJoin Semaphore 使用方法和注意事项
|
7月前
JsonUtil工具类
JsonUtil工具类
38 0
工具类-HttpClientUtil
工具类-HttpClientUtil
56 0
|
搜索推荐 安全 小程序
6个十分好用的工具类网站
6个十分好用的工具类网站
194 0
6个十分好用的工具类网站
|
安全
CollectionUtils工具类的常用方法
集合判断:   例1: 判断集合是否为空:  CollectionUtils.isEmpty(null): true  CollectionUtils.isEmpty(new ArrayList()): true    CollectionUtils.
2067 0
RedisUtils 工具类
RedisUtils 工具类
146 0