访问SharedPreferences中的数据代码如下所示
SharedPreferences sharedPreferences=getSharedPreferences("eric",Context.MODE_PRIVATE);
//getString()第二个参数为默认值,如果Preference中不存在key,将返回默认值
String name=sharedPreferences.getString("name","");
int age=sharedPreferences.getInt("age",1);
如果访问其他应用中的Preference,前提条件是该preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限。如有个<package name>为com.jxqx.action的应用使用下面语句创建了Preference。
getSharedPrefereces("eric",Context.MODE_WORLD_READABLE);
其他应用要访问上面应用的Preference,首先需要创建上面应用的Context,然后通过Context访问preference,访问preference时会使用所在包下的shared_prefs 目录找到preference;
Context otherAppsContext=createPackageContext("com.jxqx.action",Contetxt.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences.getString("eric",Context.MODE_WORLD_READABLE);
String name=sharedPreferences.getString("name","");
int age=sharedPreferences.getInt("age",0);
模块化使用SharedPreferences,建立一个类专门用于处理SharedPreferences 的方法。
package com.jxqx.common.util;
import android.content.Context;
import android.content.SharedPreferences;
public class SharePreferenceUtil {
public static final String MESSAGE_NOTIFY_KEY="message_notify";
public static final String MESSAGE_SOUND_KEY="message_sound";
public static final String SHOW_HEAD_KEY="show_head";
public static final String PULLREFRESH_SOUND_KEY="pullrefresh_sound";
private SharedPreferences sp;
private SharedPreferences.Editor editor;
//初始化sp、editor
public SharePreferenceUtil(Context context, String file) {
sp = context.getSharedPreferences(file, Context.MODE_PRIVATE);
editor = sp.edit();
}
//appId
public void setAppId(String appId){
editor.putString("appId", appId);
editor.commit();
}
public String getAppId(){
return sp.getString("appId", "");
}
//userId
public void setUserId(String userId){
editor.putString("userId", userId);
editor.commit();
}
public String getUserId(){
return sp.getString("userId", "");
}
// channel_id
public void setChannelId(String ChannelId) {
editor.putString("ChannelId", ChannelId);
editor.commit();
}
public String getChannelId() {
return sp.getString("ChannelId", "");
}
// nick
public void setNick(String nick) {
editor.putString("nick", nick);
editor.commit();
}
public String getNick() {
return sp.getString("nick", "");
}
// 头像图标
public int getHeadIcon() {
return sp.getInt("headIcon", 0);
}
public void setHeadIcon(int icon) {
editor.putInt("headIcon", icon);
editor.commit();
}
// 设置Tag
public void setTag(String tag) {
editor.putString("tag", tag);
editor.commit();
}
public String getTag() {
return sp.getString("tag", "");
}
//是否有通知
public boolean getMsgNotify() {
return sp.getBoolean(MESSAGE_NOTIFY_KEY, true);
}
public void setMsgNotify(boolean isChecked) {
editor.putBoolean(MESSAGE_NOTIFY_KEY, isChecked);
editor.commit();
}
// 新消息是否有声音
public boolean getMsgSound() {
return sp.getBoolean(MESSAGE_SOUND_KEY, true);
}
public void setMsgSound(boolean isChecked) {
editor.putBoolean(MESSAGE_SOUND_KEY, isChecked);
editor.commit();
}
// 刷新是否有声音
public boolean getPullRefreshSound() {
return sp.getBoolean(PULLREFRESH_SOUND_KEY, true);
}
public void setPullRefreshSound(boolean isChecked) {
editor.putBoolean(PULLREFRESH_SOUND_KEY, isChecked);
editor.commit();
}
// 是否显示自己头像
public boolean getShowHead() {
return sp.getBoolean(SHOW_HEAD_KEY, true);
}
public void setShowHead(boolean isChecked) {
editor.putBoolean(SHOW_HEAD_KEY, isChecked);
editor.commit();
}
// 表情翻页效果
public int getFaceEffect() {
return sp.getInt("face_effects", 3);
}
public void setFaceEffect(int effect) {
if (effect < 0 || effect > 11)
effect = 3;
editor.putInt("face_effects", effect);
editor.commit();
}
}
使用时先实例化创建一个对象。
public synchronized SharePreferenceUtil getSpUtil() {
if (mSpUtil == null)
mSpUtil = new SharePreferenceUtil(this, SP_FILE_NAME);
return mSpUtil;
}
然后通过这个对象来引用相关的方法。
private SharePreferenceUtil spUtil;
spUtil = PushApplication.getInstance().getSpUtil();