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

转载自:http://blog.csdn.net/u014727709/article/details/53390514

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想你依然心痛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值