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

         首先介绍的是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 stub
		setUpdateYear(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"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值