Android SharedPreferences的简单封装

时间:2022-02-08 05:30:02

Android SharedPreferences的简单封装

SP数据保存在/data/data/<package name>/shared_prefs目录下且以XML格式保存的。

Android SharedPreferences的简单封装

SPUtils

package com.example.shen.sputil.utils;

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

/**
* Created by shen on 2016/2/26.
*/
public class SPUtil {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;

public SPUtil(Context context, String fileName) {
preferences = context.getSharedPreferences(fileName, context.MODE_PRIVATE);
editor = preferences.edit();
}


/**
* 向SP存入指定key对应的数据
* 其中value可以是String、boolean、float、int、long等各种基本类型的值
* @param key
* @param value
*/
public void putString(String key, String value) {
editor.putString(key, value);
editor.commit();
}

public void putBoolean(String key, boolean value) {
editor.putBoolean(key, value);
editor.commit();
}

public void putFloat(String key, float value) {
editor.putFloat(key, value);
editor.commit();
}

public void putInt(String key, int value) {
editor.putInt(key, value);
editor.commit();
}

public void putLong(String key, long value) {
editor.putLong(key, value);
editor.commit();
}

/**
* 清空SP里所以数据
*/
public void clear() {
editor.clear();
editor.commit();
}

/**
* 删除SP里指定key对应的数据项
* @param key
*/
public void remove(String key) {
editor.remove(key);
editor.commit();
}

/**
* 获取SP数据里指定key对应的value。如果key不存在,则返回默认值defValue。
* @param key
* @param defValue
* @return
*/
public String getString(String key, String defValue) {
return preferences.getString(key, defValue);
}

public boolean getBoolean(String key, boolean defValue) {
return preferences.getBoolean(key, defValue);
}

public float getFloat(String key, float defValue) {
return preferences.getFloat(key, defValue);
}

public int getInt(String key, int defValue) {
return preferences.getInt(key, defValue);
}

public long getLong(String key, long defValue) {
return preferences.getLong(key, defValue);
}

/**
* 判断SP是否包含特定key的数据
* @param key
* @return
*/
public boolean contains(String key){
return preferences.contains(key);
}

}
使用

package com.example.shen.sputil.activity;

import android.app.Activity;
import android.os.Bundle;

import com.example.shen.sputil.R;
import com.example.shen.sputil.utils.SPUtil;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SPUtil spUtil=new SPUtil(this,"fileName");
spUtil.clear();
spUtil.putString("key1","value1");
spUtil.putInt("key2", 123);
spUtil.getString("key1", "");
spUtil.getInt("key2",0);
}
}