在大家有一定的 SharedPreferences 使用经验后更佳
1. 概述
SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中。例如保存登录用户的用户名和密码。只能在同一个包内使用,不能在不同的包之间使用,其实也就是说只能在创建它的应用中使用,其他应用无法使用
- 创建的存储文件保存在 /data/data//shares_prefs 文件夹下(路径也许改了,但是意思是酱紫)
2. Android native 使用
通过Context.getSharedPreferences方法获取SharedPreferences对象,参数分别为存储的文件名和存储模式
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences(DATABASE, Activity.MODE_PRIVATE);
// 获取Editor对象
Editor editor = sp.edit();
//存储数据是通过 Editor 来操作的
// 插入数据
editor.putString(key, value);
editor.commit();
// 删除数据
editor.remove(key);
editor.commit();
// 清空所有数据
editor.clear();
editor.commit();
// 查询数据
sp.getString(key, "");
2.1 SharedPreferences 存储数据方式工具类
/**
* SharedPreferences存储数据方式工具类
* @author zuolongsnail
*/
public class SharedPrefsUtil {
public final static String SETTING = "Setting";
public static void putValue(Context context,String key, int value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putInt(key, value);
sp.commit();
}
public static void putValue(Context context,String key, boolean value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putBoolean(key, value);
sp.commit();
}
public static void putValue(Context context,String key, String value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putString(key, value);
sp.commit();
}
public static int getValue(Context context,String key, int defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
int value = sp.getInt(key, defValue);
return value;
}
public static boolean getValue(Context context,String key, boolean defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
boolean value = sp.getBoolean(key, defValue);
return value;
}
public static String getValue(Context context,String key, String defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
String value = sp.getString(key, defValue);
return value;
}
}
2.2 获取SharedPreferences的两种方式
- 1 调用Context对象的getSharedPreferences()方法
- 调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.
- 2 调用Activity对象的getPreferences()方法
- 调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.
2.3 SharedPreferences的四种操作模式
Context.MODE_PRIVATE
- 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND
- 式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件
- Context.MODE_WORLD_READABLE
- 表示当前文件可以被其他应用读取.
- Context.MODE_WORLD_WRITEABLE
- 表示当前文件可以被其他应用写入.
3. Xamarin.Android 使用
在 Xamarin.Android 中,有些变化需要注意,你必须先得到 ISharedPreferences 的实例才能去使用SP,有三种方法去得到 :
Activity.GetPreferences- would get you preferences specific to that activity. Probably not what you want.
Context.GetSharedPreferences- can get you application level preferences.
PreferenceManager.DefaultSharedPreferences
- will give you an ISharedPreference instance for a given context
简单用法:
//this is an Activity.this
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutInt("number", 5);
editor.PutString("date", DateTime.Now.ToString("yyyy-MMM-dd"));
editor.Apply();//推荐使用 editor.Apply(); sp.commit()是比较旧的方法,忘记文档在哪,找到补上
如果你的代码中使用上述代码,每次app启动的时候存储的值都会变化,试试下面的代码
你会很好奇怎么设置存储模式?
var c = (Context)this;
ISharedPreferences prefs = c.GetSharedPreferences ("SimpleTest", FileCreationMode.WorldReadable);
16年的时候这个bug还没修复,先记下:
App.Prefs.Edit().PutInt( GetString( Resource.String.pref_calendarId_int ), calId ); App.Prefs.Edit().Commit();
doesn't work, while this:
var editor = App.Prefs.Edit(); editor.PutInt( GetString( Resource.String.pref_calendarId_int ), calId ); editor.Commit();
DOES work!
4 . 工具类
public class AppPreferences : ISharedPreferences, ISharedPreferencesEditor
{
private ISharedPreferences mSharedPrefs;
private ISharedPreferencesEditor mPrefsEditor;
private Context mContext;
#region ISharedPreferences impl
public IDictionary<string, object> All
{
get
{
return mSharedPrefs.All;
}
}
public IntPtr Handle
{
get
{
return mSharedPrefs.Handle;
}
}
public AppPreferences(Context context)
{
this.mContext = context;
mSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
mPrefsEditor = mSharedPrefs.Edit();
}
public bool Contains(string key)
{
throw new NotImplementedException();
}
public ISharedPreferencesEditor Edit()
{
throw new NotImplementedException();
}
public string GetString(string key, string defValue)
{
return mSharedPrefs.GetString(key, defValue);
}
public bool GetBoolean(string key, bool defValue)
{
return mSharedPrefs.GetBoolean(key, defValue);
}
public float GetFloat(string key, float defValue)
{
return mSharedPrefs.GetFloat(key, defValue);
}
public int GetInt(string key, int defValue)
{
return mSharedPrefs.GetInt(key, defValue);
}
public long GetLong(string key, long defValue)
{
return mSharedPrefs.GetLong(key, defValue);
}
public ICollection<string> GetStringSet(string key, ICollection<string> defValues)
{
return mSharedPrefs.GetStringSet(key, defValues);
}
public void RegisterOnSharedPreferenceChangeListener(ISharedPreferencesOnSharedPreferenceChangeListener listener)
{
mSharedPrefs.RegisterOnSharedPreferenceChangeListener(listener);
}
public void UnregisterOnSharedPreferenceChangeListener(ISharedPreferencesOnSharedPreferenceChangeListener listener)
{
mSharedPrefs.UnregisterOnSharedPreferenceChangeListener(listener);
}
public void Dispose()
{
mSharedPrefs.Dispose();
}
#endregion
#region ISharedPreferencesEditor impl
public void Apply()
{
mPrefsEditor.Apply();
}
public ISharedPreferencesEditor Clear()
{
return mPrefsEditor.Clear();
}
public bool Commit()
{
return mPrefsEditor.Commit();
}
public ISharedPreferencesEditor PutBoolean(string key, bool value)
{
return mPrefsEditor.PutBoolean(key, value);
}
public ISharedPreferencesEditor PutFloat(string key, float value)
{
return mPrefsEditor.PutFloat(key, value);
}
public ISharedPreferencesEditor PutInt(string key, int value)
{
return mPrefsEditor.PutInt(key, value);
}
public ISharedPreferencesEditor PutLong(string key, long value)
{
return mPrefsEditor.PutLong(key, value);
}
public ISharedPreferencesEditor PutString(string key, string value)
{
return mPrefsEditor.PutString(key, value);
}
public ISharedPreferencesEditor PutStringSet(string key, ICollection<string> values)
{
return mPrefsEditor.PutStringSet(key, values);
}
public ISharedPreferencesEditor Remove(string key)
{
return mPrefsEditor.Remove(key);
}
#endregion
}
本文详细介绍了SharedPreferences在Android中的使用方法,包括基本概念、数据存储方式、不同模式下的操作特点及Xamarin.Android平台上的应用技巧。

351

被折叠的 条评论
为什么被折叠?



