- public class SharedPreferencesHelper {
- private static final String FILE_NAME = "app_data";
- private static SharedPreferences mSharedPreferences;// 单例
- private static SharedPreferencesHelper instance;// 单例
- private SharedPreferencesHelper(Context context) {
- mSharedPreferences = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- }
- /**
- * 初始化单例
- * @param context
- */
- public static synchronized void init(Context context) {
- if (instance == null) {
- instance = new SharedPreferencesHelper(context);
- }
- }
- /**
- * 获取单例
- *
- * @return
- */
- public static SharedPreferencesHelper getInstance() {
- if (instance == null) {
- throw new RuntimeException("class should init!");
- }
- return instance;
- }
- /**
- * 保存数据
- *
- * @param key
- * @param data
- */
- public void saveData(String key, Object data) {
- String type = data.getClass().getSimpleName();
- Editor editor = mSharedPreferences.edit();
- if ("Integer".equals(type)) {
- editor.putInt(key, (Integer) data);
- } else if ("Boolean".equals(type)) {
- editor.putBoolean(key, (Boolean) data);
- } else if ("String".equals(type)) {
- editor.putString(key, (String) data);
- } else if ("Float".equals(type)) {
- editor.putFloat(key, (Float) data);
- } else if ("Long".equals(type)) {
- editor.putLong(key, (Long) data);
- }
- editor.commit();
- }
- /**
- * 得到数据
- *
- * @param key
- * @param defValue
- * @return
- */
- public Object getData(String key, Object defValue) {
- String type = defValue.getClass().getSimpleName();
- if ("Integer".equals(type)) {
- return mSharedPreferences.getInt(key, (Integer) defValue);
- } else if ("Boolean".equals(type)) {
- return mSharedPreferences.getBoolean(key, (Boolean) defValue);
- } else if ("String".equals(type)) {
- return mSharedPreferences.getString(key, (String) defValue);
- } else if ("Float".equals(type)) {
- return mSharedPreferences.getFloat(key, (Float) defValue);
- } else if ("Long".equals(type)) {
- return mSharedPreferences.getLong(key, (Long) defValue);
- }
- return null;
- }
调用方式如下
1、初始化:SharedPreferencesHelper.init(getApplicationContext()); 一般放在自己的Application的onCreate方法里面执行,保证全局初始化。
2、调用saveData和getData进行数据的存储和读取。
通过以上两步就能实现对SharedPreferences的操作,是不是很方便!当然,因为存储和读取的时候需要知道键,所以可以根据我们的需求对其进行进一步封装,如下AppData类所示:
- public class AppData {
- private String KEY_SETTING_NOTIFICATION = "key_setting_notification";
- private String KEY_SETTING_SOUND = "key_setting_sound";
- private String KEY_SETTING_VIBRATE = "key_setting_vibrate";
- private String KEY_SETTING_TESTENV = "key_setting_testenv";
- //在这里进行扩展,加入键,然后自动生成getter和setter函数,就达到了扩展的目的
- public AppData(Context context) {
- SharedPreferencesHelper.init(context);
- }
- public void setSettingNotification(boolean bFlag) {
- SharedPreferencesHelper.getInstance().saveData(
- KEY_SETTING_NOTIFICATION, bFlag);
- }
- public boolean getSettingNotification() {
- return (Boolean) SharedPreferencesHelper.getInstance().getData(
- KEY_SETTING_NOTIFICATION, false);
- }
- public void setSettingSound(boolean bFlag) {
- SharedPreferencesHelper.getInstance()
- .saveData(KEY_SETTING_SOUND, bFlag);
- }
- public boolean getSettingSound() {
- return (Boolean) SharedPreferencesHelper.getInstance().getData(
- KEY_SETTING_SOUND, false);
- }
- public void setSettingVibrate(boolean bFlag) {
- SharedPreferencesHelper.getInstance().saveData(KEY_SETTING_VIBRATE,
- bFlag);
- }
- public boolean getSettingVibrate() {
- return (Boolean) SharedPreferencesHelper.getInstance().getData(
- KEY_SETTING_VIBRATE, false);
- }
- public void setTestEnvSetting(boolean bFlag) {
- Log.d("imdata", "set testenv:" + bFlag);
- SharedPreferencesHelper.getInstance().saveData(KEY_SETTING_TESTENV,
- bFlag);
- }
- public boolean getTestEnvSetting() {
- return (Boolean) SharedPreferencesHelper.getInstance().getData(
- KEY_SETTING_TESTENV, false);
- }
调用方法如下
1、在MyApplication里面定义AppData对象:private AppData imdata = new AppData(this);
2、定义相应的方法:
- public boolean getSettingSound() {
- return imdata.getSettingSound();
- }
- public void setSettingSound(boolean bFlag) {
- imdata.setSettingSound(bFlag);
- }
通过以上封装,就非常方便和易于扩展了^_^!