Android中数据存储之SharedPreferences

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreferences是一种轻型的数据存储方式,它的本质是基于XML文件存储Key-Value键值对数据,<br/>
 * 通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。<br/>
 * SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。<br/>
 * SharedPreferences对象与SQLite数据库相比,免去了创建数据库、创建表、写SQL语句等操作,更加易用。<br/>
 * 但是SharedPreferences仅支持以下数据类型:<br/>
 * boolean<br/>
 * int<br/>
 * float<br/>
 * long<br/>
 * String<br/>
 * 但是无法进行条件查询等,所以不论SharedPreferences的数据存储操作是如何简单,<br/>
 * 它只能是存储方式的一种补充,而无法完全替代如SQLite数据库等其他数据存储方式。<br/>
 *
 * @author wangzhu
 * @date 2014年11月22日 上午10:17:49
 */
public class SharedPreferencesUtils {
    /**
     * 将内容写入SharedPrefrences中
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param value
     *            值
     */
    public static void write(Context context, String fileName, String key,
            boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 读取SharedPreferences中标记所对应的值
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param defValue
     *            默认值
     * @return
     */
    public static boolean readBoolean(Context context, String fileName,
            String key, boolean defValue) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        return preferences.getBoolean(key, defValue);
    }

    /**
     * 将内容写入SharedPrefrences中
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param value
     *            值
     */
    public static void write(Context context, String fileName, String key,
            int value) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 读取SharedPreferences中标记所对应的值
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param defValue
     *            默认值
     * @return
     */
    public static int readInt(Context context, String fileName, String key,
            int defValue) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        return preferences.getInt(key, defValue);
    }

    /**
     * 将内容写入SharedPrefrences中
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param value
     *            值
     */
    public static void write(Context context, String fileName, String key,
            long value) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * 读取SharedPreferences中标记所对应的值
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param defValue
     *            默认值
     * @return
     */
    public static long readLong(Context context, String fileName, String key,
            long defValue) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        return preferences.getLong(key, defValue);
    }

    /**
     * 将内容写入SharedPrefrences中
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param value
     *            值
     */
    public static void write(Context context, String fileName, String key,
            float value) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    /**
     * 读取SharedPreferences中标记所对应的值
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param defValue
     *            默认值
     * @return
     */
    public static float readFloat(Context context, String fileName, String key,
            float defValue) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        return preferences.getFloat(key, defValue);
    }

    /**
     * 将内容写入SharedPrefrences中
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param value
     *            值
     */
    public static void write(Context context, String fileName, String key,
            String value) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 读取SharedPreferences中标记所对应的值
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     * @param defValue
     *            默认值
     * @return
     */
    public static String readString(Context context, String fileName,
            String key, String defValue) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        return preferences.getString(key, defValue);
    }

    /**
     * 删除SharedPreferences中Key对应的值
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     * @param key
     *            标记
     */
    public static void remove(Context context, String fileName, String key) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除SharedPreferences中对应文件的所有内容
     *
     * @param context
     *            上下文对象
     * @param fileName
     *            文件名
     */
    public static void clear(Context context, String fileName) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}
时间: 2024-08-18 23:48:15

Android中数据存储之SharedPreferences的相关文章

Android中数据存储(一)

国庆没有给国家添堵,没有勾搭妹子,乖乖的写着自己的博客..... 本文将为大家介绍Android中数据存储的五种方式,数据存储可是非常重要的知识哦. 一,文件存储数据 ①在ROM存储数据 关于在ROM读写文件,可以使用java中IO流来读写,但是谷歌很人性化,直接给你封装了一下,所以就有了Context提供的这两个方法:FileInputStream openFileInput(String name); FileOutputStream openFileOutput(String name,

(十)android 中数据存储与访问——使用SharedPreferences保存数据

10.1 SharedPreferences概述 数据存储方式有五种,前面介绍的是通过IO流以文件的方式存储数据,这里学习的SharedPreferences方式保存的数据,主要保存的是用户的偏好设置. 很多时候,我们开发的程序是需要向用户提供软件参数设置功能的.用户根据自己的兴趣爱好对软件的各项参数进行配置,以符合自己的使用习惯. 例如,我们使用eclipse的时候,可以设置字体的显示颜色.大小等.Eclipse内部使用的是xml格式的文件来保存软件的配置参数. 如果我们要在安卓中保存用户在软

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

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

Android简易数据存储之SharedPreferences

Andorid提供了多种数据存储的方式,例如前面说到的“Android数据存储之SQLite的操作”是用于较复杂的数据存储.然而,如果有些简单的数据存储如果采用SQLite的方式的话会显得比较笨重.例如:记录用户是否访问过APP的欢迎页面之类的数据,如果采用SQLite的话会显得没必要而且费时费力.因此Andorid提供了另一种存储简单数据的方式SharedPreferences.SharedPreferences是一个轻量级的数据存储方式,其仅支持boolean.int.long.float.

Android中数据存储方式一:文件形式

总结在Android中,一共有数据存储的5种方式.今天做一个笔记的整理.关于以文件形式如何来保存数据. 1.在activity_main.xml设计好布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_pa

(九)android 中数据存储与访问——保存文件到手机内存

9.1手机的存储区域 手机的存储区域通常有两个地方:一:手机内部存储空间,理解成一块微硬盘/data/data/:二:外部存储空间SD卡 9.2方法捕获异常的原则 如果方法有返回值,则用try catch捕获,如果方法的返回值是Void类型,则使用throws抛出异常 9.3 上下文Context Context:是一个类,提供一些方便的api 可以得到应用程序的环境,例如:环境的包名,安装路径,资源路径,资产的路径 9.4 保存文件到手机内存——登陆界面例子程序 9.4.1 项目需求 用户登陆

处女男学Android(十四)---Android 重量级数据存储之SQLite

前言 不知不觉的Android基础系列已经写了十三篇了,这是第十四篇~上一篇blog记录了Android中的一种数据存储方案,即共享参数(Sharedpreferences)的使用(处女男学Android(十三)---Android 轻量级数据存储之SharedPreferences).最近初学如何在Android中应用SQLite,写了一个基于ListView的增删查的小例子,本篇blog就记录一下我学习到的如何在Android中操作SQLite持久化客户端数据. 初始化SQLite 关于SQ

Android 数据存储之 SharedPreferences储存

------------------------------------------SharedPreferences存储-------------------------------------------- SharedPreferences 使用键值对方式来存储数据的.当保存一条数据的时候,需要给这条数据提供一个对应的键,这样在读取数据的时候就可以通过这个键把相应的值取出. SharedPreferences 支持多种不同数据类型的存储,可以按照需要取出数值型,或者字符型的数据. ----

Android笔记——Android中数据的存储方式(二)

我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效率.如果学过JavaWeb的朋友,首先可能想到的是数据库.当然了数据库是一个方案,那么是否还有其他的解决方案呢?今天我们在讲下Android笔记——Android中数据的存储方式(一) 提到的除了SharedPreferences和Files(文本文件)以外的其他几种数据储存方式:xml文件.SQL