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,欢迎评论,欢迎指正



相关文章
|
网络协议 测试技术
【干货】UDP怎么测?
【干货】UDP怎么测?
3581 0
|
存储 测试技术 Linux
关于kunit的一点够用就行知识概念
关于kunit的一点够用就行知识概念
667 0
|
9月前
|
人工智能 Kubernetes 安全
OpenClaw 在严肃场景下的实践:迁移 Ingress NGINX
Kubernetes 官方声明说得很清楚:你只有两个月时间。
424 53
|
8月前
|
传感器 数据采集 数据可视化
数字孪生项目的开发流程
数字孪生是融合IoT、多维建模、AI仿真与实时可视化的系统工程。典型流程含需求定义、数据采集、几何/机理建模、数据融合、算法仿真、交互可视化、系统部署及持续运维,实现虚实同步、预测决策与闭环控制。(239字)
|
8月前
|
人工智能 安全 Linux
OpenClaw(Clawdbot) Linux部署全教程:本地搭建、ZeroNews接入及阿里云部署指南
OpenClaw(前身为Clawdbot)凭借“私有化运行+全权限实操”的创新定位,迅速成为AI领域的现象级开源项目。这款被称为“真正能做实事的AI”的工具,打破了传统聊天机器人的功能局限,可在本地设备或服务器上独立运行,通过WhatsApp、Telegram等常用聊天软件接收指令,完成文件读写、邮件管理、系统运维等实操任务,所有数据本地存储,隐私安全性拉满。
804 0
|
IDE 开发工具 C语言
Visual Studio 2017 安装及使用(新手)
Visual Studio 2017 安装及使用(新手)
2698 0
|
机器学习/深度学习 人工智能 算法
基于Python深度学习的【蘑菇识别】系统~卷积神经网络+TensorFlow+图像识别+人工智能
蘑菇识别系统,本系统使用Python作为主要开发语言,基于TensorFlow搭建卷积神经网络算法,并收集了9种常见的蘑菇种类数据集【"香菇(Agaricus)", "毒鹅膏菌(Amanita)", "牛肝菌(Boletus)", "网状菌(Cortinarius)", "毒镰孢(Entoloma)", "湿孢菌(Hygrocybe)", "乳菇(Lactarius)", "红菇(Russula)", "松茸(Suillus)"】 再使用通过搭建的算法模型对数据集进行训练得到一个识别精度较高的模型,然后保存为为本地h5格式文件。最后使用Django框架搭建了一个Web网页平台可视化操作界面,
1683 11
基于Python深度学习的【蘑菇识别】系统~卷积神经网络+TensorFlow+图像识别+人工智能
|
搜索推荐 小程序 数据可视化
网站用户访问分析入门:新手必懂的6个关键指标
如果你正在运营一个网站,无论是产品官网还是电商平台,用户访问分析一定是绕不开的一项工作。 但对很多刚入门的新手来说,打开一个数据平台,常常是一脸懵: PV?UV?跳出率?这些到底是什么?要看哪些指标才有用? 这篇文章,我们一起看看网站访问分析中最基础的6个核心指标,帮你快速理解并掌握基本的分析能力。
2993 0
|
Windows
.Net Framework 0x800b0109 -已处理证书链,但是在不受信任提供程序信任的根证书中终止。
.Net Framework 0x800b0109 -已处理证书链,但是在不受信任提供程序信任的根证书中终止。
2736 1
.Net Framework 0x800b0109 -已处理证书链,但是在不受信任提供程序信任的根证书中终止。