Android 之SharedPreferences存储

封装好的一个工具类、直接上代码:

/**
 * @author Jenly
 * @date 2014-8-8
 */
public class SharedPreferencesUtils {

	public static final String PREF_NAME = "org.king.pref_name_jenly";

	public static SharedPreferences getSharedPreferences(Context context){
		return getSharedPreferences(context, PREF_NAME);
	}

	public static SharedPreferences getSharedPreferences(Context context, String prefName){
		return context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
	}

	//--------------------------Int
	public static void putInt(Context context,String key,int value){
		getSharedPreferences(context).edit().putInt(key, value).commit();
	}

	public static int getInt(Context context,String key,int defValue){
		return getSharedPreferences(context).getInt(key, defValue);
	}

	public static int getInt(Context context,String key){
		return getInt(context,key,0);
	}

	//--------------------------Float
	public static void putFloat(Context context,String key,float value){
		getSharedPreferences(context).edit().putFloat(key, value).commit();
	}

	public static float getFloat(Context context,String key,float defValue){
		return getSharedPreferences(context).getFloat(key, defValue);
	}
	public static float getFloat(Context context,String key){
		return getFloat(context, key, 0);
	}

	//--------------------------Long
	public static void putLong(Context context,String key,long value){
		getSharedPreferences(context).edit().putLong(key, value).commit();
	}

	public static long getLong(Context context,String key,long defValue){
		return getSharedPreferences(context).getLong(key, defValue);
	}
	public static long getLong(Context context,String key){
		return getLong(context, key, 0);
	}

	//--------------------------Boolean
	public static void putBoolean(Context context,String key,boolean value){
		getSharedPreferences(context).edit().putBoolean(key, value).commit();
	}

	public static boolean getBoolean(Context context,String key,boolean defValue){
		return getSharedPreferences(context).getBoolean(key, defValue);
	}
	public static boolean getBoolean(Context context,String key){
		return getBoolean(context, key, false);
	}

	//--------------------------String
	public static void putString(Context context,String key,String value){
		getSharedPreferences(context).edit().putString(key, value).commit();
	}

	public static String getString(Context context,String key,String defValue){
		return getSharedPreferences(context).getString(key, defValue);
	}

	public static String getString(Context context,String key){
		return getString(context, key, null);
	}

	//--------------------------Map
	@SuppressLint("CommitPrefEdits")
	public static void putMapString(Context context,Map<String,String> map){
		if(map!=null){
			SharedPreferences.Editor editor = getSharedPreferences(context).edit();

			for(Entry<String, String> entry: map.entrySet()){
				if(!TextUtils.isEmpty(entry.getKey()))
					editor.putString(entry.getKey(), entry.getValue());
			}

			editor.commit();

		}
	}

	//--------------------------Increase int
	/**
	 * 自增(默认自增1)
	 * @param context
	 * @param key
	 */
	public static void increase(Context context,String key){
		increase(context, key,1);
	}

	/**
	 * 自增
	 * @param context
	 * @param key
	 * @param deltaValue 增量值(基数)
	 */
	public static void increase(Context context,String key,int deltaValue){
		getSharedPreferences(context).edit().putInt(key,getInt(context, key)+deltaValue).commit();
	}

}
时间: 2024-08-09 09:40:17

Android 之SharedPreferences存储的相关文章

使用AES加密进行Android的SharedPreferences存储

1.概述 SharedPreferences是Android提供用来存储一些简单配置信息的机制,其以KEY-VALUE对的方式进行存储,以便我们可以方便进行读取和存储.主要可以用来存储应用程序的欢迎语.常量参数或登录账号密码等. 2.实例 (1)创建项目SharedPreferencesDemo项目 (2)编辑主界面的布局文件main.xml如下: [xhtml] view plaincopy <?xml version="1.0" encoding="utf-8&qu

Android之SharedPreferences(存储本地数据)

写入数据: // 更新写入本地缓存(用于下次无法定位时使用) // 1.实例化SharedPreferences对象(第一步) SharedPreferences mySharedPreferences = getSharedPreferences("cityinfo", MODE_PRIVATE); // 2. 实例化SharedPreferences.Editor对象(第二步) SharedPreferences.Editor editor = mySharedPreference

【Mark】Android应用开发SharedPreferences存储数据的使用方法

Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据. 1.使用SharedPreferences保存数据方法如下: //实例化SharedPreferences对象(第一步) SharedPreferences mySharedPreferences=

处女男学Android(十三)---Android 轻量级数据存储之SharedPreferences

一.前言 转载请标明出处:http://blog.csdn.net/wlwlwlwl015/article/details/42437007 初学Android的时候在Activity之间传值我都是用Intent+Bundle这种模式去实现的,刚开始觉得没什么,后来渐渐发现了弊端,就是说只能逐层传递,当我的好几个Activity都需要从一个Activity中取数据的时候,这样就显得相当局限了,传来传去的即麻烦,又不合理,后来就想在Android中有没有web开发中类似于Session的东西,只要

Android 使用SharedPreferences进行数据存储和读取数据

很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件或者xml进行保存.如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPreferen

Android SharedPreferences存储

一 概念 SharedPreferences存储方式是Android中存储轻量级数据的一种方式.SharedPreferences存储主要用来存储一些简单的配置信息,内部以Map方式进行存储,因此需要使用键值对提交和保存数据,保存的数据以xml格式存放在本地的/data/data/<package name>/shares_prefs文件夹下. 二 特点 1,        使用简单,便于存储轻量级的数据: 2,        只支持Java基本数据类型,不支持自定义数据类型: 3,     

Android数据存储之SharedPreferences存储

安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布尔型等.这些配置最后会保存在一个XML文件中,每次打开应用时,这些保存的信息就会被加载进来,我们也可以在“管理应用程序”中将这些缓存数据清除. SharedPreferences接口的常用方法如下: SharedPreferences接口类方法 No 方法 类型 描述 1 public abstra

Android的移动存储之SharedPreferences

在Android系统中提供了多种存储技术.通过这些存储技术可以将数据存储在各种存储介质上.比如sharedpreferences可以将数据保存着应用软件的私有存储区,这些存储区的数据只能被写入这些数据的软件读取.当然Android还支持文件存储.SQLite数据库和Content Provider.在这里我们将对sharedpreferences存储方式进行介绍. SharedPreferences是一种轻量级的数据存储方式,学过Web开发的同学,可以想象它是一个小小的Cookie.它可以用键值

Android入门(九)文件存储与SharedPreferences存储

原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然,除了这三种方式之外,你还可以将数据保存在手机的 SD卡中,不过使用文件.SharedPreference或数据库来保存数据会相 对更简单一些,而且比起将数据保存在 SD卡中会更加的安全 一.文件存储 1.将数据存储到文件中 Context类中提供了一个 openFileOutput ()方法,可以用