import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; /**
* SharedPreferences的工具类
* @author wangfubin
*/
public class Sp { private static String name = "config"; /**
* 获取SharedPreferences实例对象
*
* @param context
* @return
*/
public static SharedPreferences getSharedPreference(Context context) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
} /**
* 保存一个Boolean类型的值!
* @param context
* @param key
* @param value
* @return
*/
public static boolean putBoolean(Context context, String key, Boolean value) {
SharedPreferences sharedPreference = getSharedPreference(context);
Editor editor = sharedPreference.edit();
editor.putBoolean(key, value);
return editor.commit();
}
/**
* 保存一个int类型的值!
* @param context
* @param key
* @param value
* @return
*/
public static boolean putInt(Context context, String key, int value) {
SharedPreferences sharedPreference = getSharedPreference(context);
Editor editor = sharedPreference.edit();
editor.putInt(key, value);
return editor.commit();
}
/**
* 保存一个float类型的值!
* @param context
* @param key
* @param value
* @return
*/
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences sharedPreference = getSharedPreference(context);
Editor editor = sharedPreference.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**
* 保存一个long类型的值!
* @param context
* @param key
* @param value
* @return
*/
public static boolean putLong(Context context, String key, long value) {
SharedPreferences sharedPreference = getSharedPreference(context);
Editor editor = sharedPreference.edit();
editor.putLong(key, value);
return editor.commit();
}
/**
* 保存一个String类型的值!
* @param context
* @param key
* @param value
* @return
*/
public static boolean putString(Context context, String key, String value) {
SharedPreferences sharedPreference = getSharedPreference(context);
Editor editor = sharedPreference.edit();
editor.putString(key, value);
return editor.commit();
} /**
* 获取String的value
*
* @param context
* @param key
* 名字
* @param defValue
* 默认值
* @return
*/
public static String getString(Context context, String key, String defValue) {
SharedPreferences sharedPreference = getSharedPreference(context);
return sharedPreference.getString(key, defValue);
} /**
* 获取int的value
*
* @param context
* @param key
* 名字
* @param defValue
* 默认值
* @return
*/
public static int getInt(Context context, String key, int defValue) {
SharedPreferences sharedPreference = getSharedPreference(context);
return sharedPreference.getInt(key, defValue);
} /**
* 获取float的value
*
* @param context
* @param key
* 名字
* @param defValue
* 默认值
* @return
*/
public static float getFloat(Context context, String key, Float defValue) {
SharedPreferences sharedPreference = getSharedPreference(context);
return sharedPreference.getFloat(key, defValue);
} /**
* 获取boolean的value
*
* @param context
* @param key
* 名字
* @param defValue
* 默认值
* @return
*/
public static boolean getBoolean(Context context, String key,
Boolean defValue) {
SharedPreferences sharedPreference = getSharedPreference(context);
return sharedPreference.getBoolean(key, defValue);
} /**
* 获取long的value
*
* @param context
* @param key
* 名字
* @param defValue
* 默认值
* @return
*/
public static long getLong(Context context, String key, long defValue) {
SharedPreferences sharedPreference = getSharedPreference(context);
return sharedPreference.getLong(key, defValue);
} }

SharedPreferences的工具类的更多相关文章

  1. Android 分享一个SharedPreferences的工具类,方便保存数据

    我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe ...

  2. SharedPreferences的工具类,使用起来方便、快捷

    SharedPreferences的工具类,使用起来方便.快捷:上代码:import android.content.Context;import android.content.SharedPref ...

  3. 写了个SharedPreferences的工具类(带加密)

    /* * Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com ) * * Licensed under the Apache License, V ...

  4. Android修改默认SharedPreferences文件的路径,SharedPreferences常用工具类

    import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; ...

  5. Android之SharedPreferences两个工具类

    相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...

  6. Android 中替代 sharedpreferences 工具类的实现

    Android 中替代 sharedpreferences 工具类的实现 背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit. ...

  7. java工具类

    1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...

  8. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  9. SharePreference 工具类封装

    import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;imp ...

随机推荐

  1. centos 安装cacti监控

    CentOS 6下Cacti搭建文档 安装依赖关系 yum -y install mysql-devel httpd php php-pdo php-snmp php-mysql lm_sensors ...

  2. mysql 创建用户与授权、修改密码

    mysql版本:5.6.35 1.创建用户 #foo表示你要建立的用户名,后面的123表示密码, #localhost限制在固定地址localhost登陆 CREATE USER foo@localh ...

  3. PNPOLY - Point Inclusion in Polygon Test

    https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html The C Code Here is the code, ...

  4. js变量,语句

  5. JavaScript动态加载资源【js|css】示例代码

    在开发过程中会用到各种第三方的插件,或者自己写在单独文件中的js方法库或者css样式,在html头部总是需要写一大堆的script和link标签,如果想要自己实现动态的引入资源文件,可以使用开源的re ...

  6. android 一些数据转换方法

    android 一些数据转换方法 package com.ai9475.util; import android.content.Context; import android.content.res ...

  7. HDU 5863 cjj's string game

    $dp$,矩阵加速. 设$dp[i][j][0]$表示:长度为$i$的两个字符串,之前还未出现过长度为$m$相同的,目前为止最后$j$个是相同的. 设$dp[i][j][1]$表示:长度为$i$的两个 ...

  8. JQuery:以name为条件查找

    例如:$(":input[name='keleyi']") 表示查找的是name为keleyi的表单. var a=$(t).prev(":input[name='dis ...

  9. 《JS权威指南学习总结--7.9 ES5中的数组方法》

    内容要点: ES5中定义了9个新的数组方法来遍历.映射.过滤.检测.简化和搜索数组. 概述:首先,大多数方法的第一个参数接收一个函数,并且对数组的每个元素(或一个元素)调用一次该函数. 如果是稀疏数组 ...

  10. 【Python】生成器和递归

    l=[1, 2, 3, 4, 5, 6] 如果l求和,毫无疑问可以使用递归,比如可以这样: def sum(l): res = 0 for i in l: if not isinstance(i, l ...