getSharedPreferences()與getSharedPreferences()與getDefaultSharedPreferences()的區別
https://blog.csdn.net/ah200614435/article/details/7869681
一直迷惑於這三個方法的關係,最近忙完項目,好好的分析一下。
如果你熟悉Context那麼你可能知道Context當中有這樣一個方法:(關於Context的說明)
一、getSharedPreferences(String name, int mode)
abstract SharedPreferences | getSharedPreferences(String name, int mode) |
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you
can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
得到名為‘name’的偏好文件。同時你可以更改和返回他的值。任何調用者在調用同樣名字的偏好文件時隻有一個實例返回,這就意味著這些調用者都可以看到其他調用者做出的更改。
這個函數的參數如下:
Parameters
Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). mode: |
|
Operating mode. Use 0 or MODE_PRIVATE for
the default operation, MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE to
control permissions. The bit MODE_MULTI_PROCESS can
also be used if multiple processes are mutating the same SharedPreferences file. MODE_MULTI_PROCESS is
always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions. |
name為本組件的配置文件名( 自己定義,也就是一個文件名),當這個文件不存在時,直接創建,如果已經存在,則直接使用,
mode為操作模式,默認的模式為0或MODE_PRIVATE,還可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE
mode指定為MODE_PRIVATE,則該配置文件隻能被自己的應用程序訪問。
mode指定為MODE_WORLD_READABLE,則該配置文件除了自己訪問外還可以被其它應該程序讀取。
mode指定為MODE_WORLD_WRITEABLE,則該配置文件除了自己訪問外還可以被其它應該程序讀取和寫入
二、PreferenceManager的方法getSharedPreferences()
這個方法我們可以通過查看其源碼:
- * Gets a SharedPreferences instance that preferences managed by this will
- * use.
- *
- * @return A SharedPreferences instance pointing to the file that contains
- * the values of preferences that are managed by this.
- */
- public SharedPreferences getSharedPreferences() {
- if (mSharedPreferences == null) {
- mSharedPreferences = mContext.getSharedPreferences(mSharedPreferencesName,
- mSharedPreferencesMode);
- }
- return mSharedPreferences;
- }
- /**
- * Gets a SharedPreferences instance that preferences managed by this will
- * use.
- *
- * @return A SharedPreferences instance pointing to the file that contains
- * the values of preferences that are managed by this.
- */
- public SharedPreferences getSharedPreferences() {
- if (mSharedPreferences == null) {
- mSharedPreferences = mContext.getSharedPreferences(mSharedPreferencesName,
- mSharedPreferencesMode);
- }
- return mSharedPreferences;
- }
這個方法是一個普通的方法,必須有PreferenceManager的實例調用才行,因此我們再按圖索驥找找其構造方法。
- /**
- * This constructor should ONLY be used when getting default values from
- * an XML preference hierarchy.
- * <p>
- * The {@link PreferenceManager#PreferenceManager(Activity)}
- * should be used ANY time a preference will be displayed, since some preference
- * types need an Activity for managed queries.
- */
- private PreferenceManager(Context context) {
- init(context);
- }
- private void init(Context context) {
- mContext = context;
- setSharedPreferencesName(getDefaultSharedPreferencesName(context));
- }
- /**
- * This constructor should ONLY be used when getting default values from
- * an XML preference hierarchy.
- * <p>
- * The {@link PreferenceManager#PreferenceManager(Activity)}
- * should be used ANY time a preference will be displayed, since some preference
- * types need an Activity for managed queries.
- */
- private PreferenceManager(Context context) {
- init(context);
- }
- private void init(Context context) {
- mContext = context;
- setSharedPreferencesName(getDefaultSharedPreferencesName(context));
- }
- /**
- * Sets the name of the SharedPreferences file that preferences managed by this
- * will use.
- *
- * @param sharedPreferencesName The name of the SharedPreferences file.
- * @see Context#getSharedPreferences(String, int)
- */
- public void setSharedPreferencesName(String sharedPreferencesName) {
- mSharedPreferencesName = sharedPreferencesName;
- mSharedPreferences = null;
- }
- /**
- * Sets the name of the SharedPreferences file that preferences managed by this
- * will use.
- *
- * @param sharedPreferencesName The name of the SharedPreferences file.
- * @see Context#getSharedPreferences(String, int)
- */
- public void setSharedPreferencesName(String sharedPreferencesName) {
- mSharedPreferencesName = sharedPreferencesName;
- mSharedPreferences = null;
- }
- private static String getDefaultSharedPreferencesName(Context context) {
- return context.getPackageName() + "_preferences";
- }
- private static String getDefaultSharedPreferencesName(Context context) {
- return context.getPackageName() + "_preferences";
- }
由以上方法,我們可以知道,最終我們調用getSharedPreferences()方法得到的是一個名為”yourpackageName_preferences“的偏好。同時其mode為默認私有。
三、getDefaultSharedPreferences方法
- /**
- * Gets a SharedPreferences instance that points to the default file that is
- * used by the preference framework in the given context.
- *
- * @param context The context of the preferences whose values are wanted.
- * @return A SharedPreferences instance that can be used to retrieve and
- * listen to values of the preferences.
- */
- public static SharedPreferences getDefaultSharedPreferences(Context context) {
- return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
- getDefaultSharedPreferencesMode());
- }
- private static String getDefaultSharedPreferencesName(Context context) {
- return context.getPackageName() + "_preferences";
- }
- private static int getDefaultSharedPreferencesMode() {
- return Context.MODE_PRIVATE;
- }
- /**
- * Gets a SharedPreferences instance that points to the default file that is
- * used by the preference framework in the given context.
- *
- * @param context The context of the preferences whose values are wanted.
- * @return A SharedPreferences instance that can be used to retrieve and
- * listen to values of the preferences.
- */
- public static SharedPreferences getDefaultSharedPreferences(Context context) {
- return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
- getDefaultSharedPreferencesMode());
- }
- private static String getDefaultSharedPreferencesName(Context context) {
- return context.getPackageName() + "_preferences";
- }
- private static int getDefaultSharedPreferencesMode() {
- return Context.MODE_PRIVATE;
- }
這個方法是靜態的,因此可以直接調用,同時它與我們調用getSharedPreferences()方法得到的返回值是一樣的,隻是調用的方式不同罷了。
四、SharedPreferences到底是什麼
它是一個輕量級的存儲類,特別適合用於保存軟件配置參數。使用SharedPreferences保存數據,其背後是用xml文件存放數據,文件存放在/data/data/<package name>/shared_prefs目錄下:
- SharedPreferences sharedPreferences = getSharedPreferences("TEST", Context.MODE_PRIVATE);
- Editor editor = sharedPreferences.edit();//獲取編輯器
- editor.putString("name", "Yang");
- editor.putInt("sex", "boy");
- editor.commit();//提交修改
- SharedPreferences sharedPreferences = getSharedPreferences("TEST", Context.MODE_PRIVATE);
- Editor editor = sharedPreferences.edit();//獲取編輯器
- editor.putString("name", "Yang");
- editor.putInt("sex", "boy");
- editor.commit();//提交修改
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <string name="name">Yang</string>
- <int name="sex">boy</string>
- </map>
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <string name="name">Yang</string>
- <int name="sex">boy</string>
- </map>
因為SharedPreferences背後是使用xml文件保存數據,getSharedPreferences(name,mode)方法的第一個參數用於指定該文件的名稱,名稱不用帶後綴,後綴會由Android自動加上。方法的第二個參數指定文件的操作模式,共有四種操作模式,這四種模式前麵介紹使用文件方式保存數據時已經講解過。如果希望SharedPreferences背後使用的xml文件能被其他應用讀和寫,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE權限。
另外Activity還提供了另一個getPreferences(mode)方法操作SharedPreferences,這個方法默認使用當前類不帶包名的類名作為文件的名稱。
如果訪問其他應用中的Preference,前提條件是:該preference創建時指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE權限。如:有個<package
name>為cn.yang.action的應用使用下麵語句創建了preference。
getSharedPreferences("TEST", Context.MODE_WORLD_READABLE);
其他應用要訪問上麵應用的preference,首先需要創建上麵應用的Context,然後通過Context 訪問preference ,訪問preference時會在應用所在包下的shared_prefs目錄找到preference :
Context otherAppsContext = createPackageContext("cn.yang.action", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("TEST", Context.MODE_WORLD_READABLE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("sex", "");
如果不通過創建Context訪問其他應用的preference,也可以以讀取xml文件方式直接訪問其他應用preference對應的xml文件,如:
File xmlFile = new File(“/data/data/<package name>/shared_prefs/itcast.xml”);//<package name>應替換成應用的包名。
最後更新:2017-04-04 07:03:39
上一篇:
Core Data淺談係列之六 : 驗證用戶輸入
下一篇:
iOS開發那些事--創建基於故事板的iOS 6的HelloWorld
isalnum <ctype.h> <cctype>
數據擦除軟件與物理破壞:數據刪除的可持續方式
GridView的RowCommand事件中取得行索引 技巧
阿裏雲個人免費套餐6個月,每天共享一枚,先到先得!
阿裏雲5周年,億萬回饋,感恩有你
收中華人民共和國稅收征收管理法(主席令第四十九號) 2015年8月15日 - 會關於修改〈中華人民共和國文物保護法〉等十二部法律的決定》(主席令第...第八十九條 納稅人、扣繳義務人可以委托稅務代理人代為辦理稅務事宜。 第...起相關人物 祁中華人民共和國
計算機常識--框架、編程語言篇
貧血模型與領域架構模式
mac os x下的一些小技巧
你需要知道知道這幾個因素會不利於關鍵詞排名優化