android数据存储之--------- SharedPreferences

来源:互联网 发布:李聪明 知乎 编辑:程序博客网 时间:2024/05/19 03:44

         首先介绍的是SharedPreferences,它是Android提供用来存储一些简单的配置信息的一种机制,例如,一些默认欢迎语、登录的用户名和密码等。其以键值对的方式存储,保存在data/data/项目名字/shared_prefs/********.xml,使得我们可以很方便的读取和存入,它提供了Android平台常规的Long长 整形、Int整形、String字符串型的保存

一个例子MySharedPreferences:它保存了一个年月日信息和一个布尔值信息

/************************************************************************************************* * 版权所有 (C)2012,   *  * 文件名称:MySharedPreferences.java * 内容摘要:xml数据存储类 * 当前版本: * 作         者:  * 完成日期:2013-01-17 * 修改记录: * 修改日期: * 版   本  号: * 修   改  人: * 修改内容: ************************************************************************************************/package com.kk.videoplayer.update;import java.util.Calendar;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.util.Log;public class MySharedPreferences {private  Context context = null;private  SharedPreferences sp = null;private Editor editor = null;public static final String MySharedPreferencesName= "update_shared_preferences_data";public static final String Key_Update_Year = "Key_Update_Year";public static final int Value_Update_Year = 2013;public static final String Key_Update_Month = "Key_Update_Month";public static final int Value_Update_Month = 1;public static final String Key_Update_Day = "Key_Update_Day";public static final int Value_Update_Day = 1;public static final String Key_Is_Have_New_Version = "Key_Is_Have_New_Version";public static final boolean Value_Is_Have_New_Version = false;private int mYear;private int mMonth;private int mDay;private int mYearInSP;private int mMonthInSP;private int mDayInSP;private boolean isNeedToUpdateInDate;public static final int leastDateIntervalToUpdate = 7; public MySharedPreferences(Context context){this.context = context;this.sp = this.context.getSharedPreferences(MySharedPreferencesName,0);this.editor =  sp.edit();this.isNeedToUpdateInDate = false;getCalendar();getCalendarInSP();}public int getUpdateYear(){int update_year = sp.getInt(Key_Update_Year, Value_Update_Year);return update_year;}public void setUpdateYear(int updateYear){editor.putInt(Key_Update_Year, updateYear);editor.commit();}public int getUpdateMonth(){int update_month = sp.getInt(Key_Update_Month, Value_Update_Month);return update_month;}public void setUpdateMonth(int updateMonth){editor.putInt(Key_Update_Month, updateMonth);editor.commit();}public int getUpdateDay(){int update_day = sp.getInt(Key_Update_Day, Value_Update_Day);return update_day;}public void setUpdateDay(int updateDay){editor.putInt(Key_Update_Day, updateDay);editor.commit();}public boolean getIsHaveNewVersion(){boolean isHaveNewVersion = sp.getBoolean(Key_Is_Have_New_Version, Value_Is_Have_New_Version);return isHaveNewVersion;}public void setIsHaveNewVersion(boolean isHaveNewVersion){editor.putBoolean(Key_Is_Have_New_Version, isHaveNewVersion);editor.commit();}public void getCalendar(){ final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR);  mMonth = c.get(Calendar.MONTH)+1; mDay = c.get(Calendar.DAY_OF_MONTH); Log.i(UpdateUtil.TAG,"-----mYear:"+mYear +"----mMonth"+mMonth +"----mDay"+mDay);}public void getCalendarInSP(){mYearInSP = getUpdateYear();mMonthInSP = getUpdateMonth();mDayInSP = getUpdateDay();Log.i(UpdateUtil.TAG,"-----mYearInSP:"+mYearInSP +"----mMonthInSP"+mMonthInSP +"----mDayInSP"+mDayInSP);}public boolean isNeedToUpdateOfCalendar(){if(mYear > mYearInSP){isNeedToUpdateInDate = true;}else if(mYear == mYearInSP){if(mMonth > mMonthInSP){isNeedToUpdateInDate = true;}else if(mMonth == mMonthInSP){if(mDay -leastDateIntervalToUpdate > mDayInSP){isNeedToUpdateInDate = true;}}}Log.i(UpdateUtil.TAG,"-----isNeedToUpdateInDate:"+isNeedToUpdateInDate);if(isNeedToUpdateInDate == true){updateDataInSP();}return isNeedToUpdateInDate;}public void updateDataInSP() {// TODO Auto-generated method stubsetUpdateYear(mYear);setUpdateMonth(mMonth);setUpdateDay(mDay);}}


第二个例子:几个关键的方法

//获取SharedPreferences对象                

Context ctx = TestSharePreferencesActivity.this;                

SharedPreferences sp = ctx.getSharedPreferences("SP", MODE_PRIVATE);

//存入数据

Editor editor = sp.edit();

editor.putString("STRING_KEY", "string0");

editor.putInt("INT_KEY", 0);

editor.putBoolean("BOOLEAN_KEY", true);

editor.commit();

Log.d(TAG, sp.getString("STRING_KEY", "none"));

Log.d(TAG, "int"+ sp.getInt("INT_KEY", 0));

//Log.d(TAG, sp.getInt((INT_KEY, 0));

Log.d(TAG, "boolean" + sp.getBoolean("BOOLEAN_KEY", true));

        

//如果NOT_EXIST不存在,则返回值为"none"

Log.d(TAG, sp.getString("NOT_EXIST", "none"));