【安卓小笔记】SharedPreferences存取数据

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

在项目使用中一般对其封装使用。

public class SharedPreUtil{
    private static final String SharedPreferenceName = "Demo";
    private static SharePreferences sp;
    private static SharePreferences.Editor editor;

    private static SharedPreferences getSharedPre(Context context){
        if(sp == null){
            sp = context.getSharePreferences(SharedPreferenceName,Context.MODE_PRIVATE);
            editor = sp.edit();
        }
    }
    public static void putIntValue(Context context,String name,int value){
        getSharedPre(context);
        editor.putInt(name,value);
        editor.commit();
    }
    public static void getIntValue(Context context,String name,int defaultValue){
        getSharedPre(context);
        return sp.getInt(name,defaultValue)1;
    }
    ....
}

然后在使用时可以直接调用来存取数据

//存
SharedPreUtil.putIntValue(this,"data","100");
//取   需要传一个默认值
int data = SharedPreUtil.getIntValue(this,"data",0);

这样就可以减少很多代码量。SharedPreferences还需要了解获取的两种方式和四种操作模式:

获取SharedPreferences的两种方式:

1 调用Context对象的getSharedPreferences()方法
调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.
2 调用Activity对象的getPreferences()方法
调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.

SharedPreferences的四种操作模式:

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.bdvcd.app.utils; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; /** * 文件名:SharedPrefsUtil.java App存储数据工具类 * 版本信息:V1.0 * 日期:2013-03-11 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class SharedPrefsUtil { /** 数据存储的XML名称 **/ public final static String SETTING = "bdvcd"; /** * 存储数据(Long) * * @param context * @param key * @param value */ public static void putLongValue(Context context, String key, long value) { Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE) .edit(); sp.putLong(key, value); sp.commit(); } /** * 存储数据(Int) * * @param context * @param key * @param value */ public static void putIntValue(Context context, String key, int value) { Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE) .edit(); sp.putInt(key, value); sp.commit(); } /** * 存储数据(String) * * @param context * @param key * @param value */ public static void putStringValue(Context context, String key, String value) { Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE) .edit(); sp.putString(key, value); sp.commit(); } /** * 存储数据(boolean) * * @param context * @param key * @param value */ public static void putBooleanValue(Context context, String key, boolean value) { Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE) .edit(); sp.putBoolean(key, value); sp.commit(); } /** * 取出数据(Long) * * @param context * @param key * @param defValue * @return */ public static long getLongValue(Context context, String key, long defValue) { SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE); long value = sp.getLong(key, defValue); return value; } /** * 取出数据(int) * * @param context * @param key * @param defValue * @return */ public static int getIntValue(Context context, String key, int defValue) { SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE); int value = sp.getInt(key, defValue); return value; } /** * 取出数据(boolean) * * @param context * @param key * @param defValue * @return */ public static boolean getBooleanValue(Context context, String key, boolean defValue) { SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE); boolean value = sp.getBoolean(key, defValue); return value; } /** * 取出数据(String) * * @param context * @param key * @param defValue * @return */ public static String getStringValue(Context context, String key, String defValue) { SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE); String value = sp.getString(key, defValue); return value; } /** * 清空所有数据 * * @param context * @param key * @param defValue * @return */ public static void clear(Context context) { Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE) .edit(); sp.clear(); sp.commit(); } /** * 清空所有数据 * * @param context * @param key * @param defValue * @return */ public static void remove(Context context, String key) { Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE) .edit(); sp.remove(key); sp.commit(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值