Android存储之SharedPreferences

Android数据存储之SharedPreferences


SharedPreferences对象初始化

SharedPreferences mSharedPreferences = getSharedPreferences(getPackageName(),MODE_PRIVATE);
  • 第一个参数是创建的文件名,可以随便命名,这里是直接获取应用包名
  • 第二个参数是操作权限 MODE_PRIVATE表示私有的
  • 读取储数据可以直接调用getXxx即可,写数据的时候需要先获取编辑对象,然后执行putXxx,最后需要commit提交。

String类型数据的存取

存储

 SharedPreferences.Editor mEdit = mSharedPreferences.edit();    
    mEdit.putString("StringData", "Hello String");
    mEdit.commit();

读取

   String stringData = mSharedPreferences.getString("StringData", "Default");

boolean类型数据的存取

存储

 SharedPreferences.Editor mEdit = mSharedPreferences.edit();
 mEdit.putBoolean("booleanData", true);
 mEdit.commit();

读取

    boolean booleanData = mSharedPreferences.getBoolean("booleanData", false);

Float类型数据的存取

存储

    SharedPreferences.Editor mEdit = mSharedPreferences.edit();
    mEdit.putFloat("floatData", 1.0f);
    mEdit.commit();

读取

    Float floatData = mSharedPreferences.getFloat("floatData", 0.0f);

int类型数据的存取

存储

    SharedPreferences.Editor mEdit = mSharedPreferences.edit();
    mEdit.putInt("intData", 1);
    mEdit.commit();

读取

    int intData = mSharedPreferences.getInt("intData", 0);

long类型数据的存取

存储

    SharedPreferences.Editor mEdit = mSharedPreferences.edit();
    mEdit.putLong("longData",1L);
    mEdit.commit();

读取

    long longData = mSharedPreferences.getLong("longData", 0);

工具类

  • 封装好的工具类,明白SharedPreferences的存储原理以后,可以直接拿过来用,很方便。
package ktalk.kong.qingwei.sharedprifence.utils;

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

/**
 * Created by qingwei on 2015/7/20.
 */
public class SPUtils {

private static SPUtils mSPUtils;
private SharedPreferences mSharedPreferences;
private Editor mEdit;

private SPUtils(Context context) {
    mSharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
    mEdit = mSharedPreferences.edit();
    }

    public static SPUtils getInstance(Context context) {
    if (null == mSPUtils) {
    mSPUtils = new SPUtils(context);
    }
    return mSPUtils;
    }

    /**
     * String
     * ***********************************************************************
     */
    public void putString(String key, String value) {
    mEdit.putString(key, value).commit();
    }

    public String getString(String key, String value) {
    return mSharedPreferences.getString(key, value);
    }

    /**
     * boolean
     * ***********************************************************************
     */
    public void putBoolean(String key, boolean value) {
    mEdit.putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key, boolean value) {
    return mSharedPreferences.getBoolean(key, value);
    }

    /**
     * float
     * ***********************************************************************
     */
    public void putFloat(String key, float value) {
    mEdit.putFloat(key, value).commit();
    }

    public Float getFloat(String key, float value) {
    return mSharedPreferences.getFloat(key, value);
    }

    /**
     * int
     * ***********************************************************************
     */
    public void putInt(String key, int value) {
    mEdit.putInt(key, value).commit();
    }

    public int getInt(String key, int value) {
    return mSharedPreferences.getInt(key, value);
    }

    /**
     * long
     * ***********************************************************************
     */
    public void putLong(String key, long value) {
    mEdit.putLong(key, value).commit();
    }

    public long getLong(String key, long value) {
    return mSharedPreferences.getLong(key, value);
    }
}
  • 用法
SPUtils.getInstance(this).putString("stringData", "Hello String");
String stringData = SPUtils.getInstance(this).getString("stringData", "default")

posted on 2015-07-20 22:13  封起De日子  阅读(137)  评论(0编辑  收藏  举报

导航