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 | 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()). name为本组件的配置文件名(如果想要与本应用程序的其他组件共享此配置文件,可以用这个名字来检索到这个配置文件)。 |
---|---|
mode | Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_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. mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和 MODE_WORLD_WRITEABLE。 |
MODE_PRIVATE
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
MODE_MULTI_PROCESS
Retrieve a SharedPreferences
object for accessing preferences that are private to this activity. This simply calls the underlyinggetSharedPreferences(String, int)
method by passing in this activity's class name as the preferences name.
mode | Operating mode. Use MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. |
---|
访问本应用程序的SharedPreferences:package com.teleca.util; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class SharedPreferencesResolver { SharedPreferences.Editor editor; Context context; SharedPreferences sp; public SharedPreferencesResolver(Context c, String name,int mode) { context = c; sp = context.getSharedPreferences(name, mode); } public final boolean contains(String key) { return sp.contains(key); } public final Map<String, ?> getAll() { return sp.getAll(); } public final String getValue(String key,String defaultValue){ return sp.getString(key, defaultValue); } public final boolean getValue(String key,boolean defaultValue){ return sp.getBoolean(key, defaultValue); } public final float getValue(String key,float defaultValue){ return sp.getFloat(key, defaultValue); } public final int getValue(String key,int defaultValue){ return sp.getInt(key, defaultValue); } public final long getValue(String key,long defaultValue){ return sp.getLong(key, defaultValue); } public final void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) { sp.registerOnSharedPreferenceChangeListener(listener); } public final void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) { sp.unregisterOnSharedPreferenceChangeListener(listener); } public final void OpenEditor() { if(editor==null) editor = sp.edit(); } public final void clearEditor() { editor.clear(); } public final void putValue(String key, String value) { editor.putString(key, value); } public final void putValue(String key, boolean value) { editor.putBoolean(key, value); } public final void putValue(String key, float value) { editor.putFloat(key, value); } public final void putValue(String key, int value) { editor.putInt(key, value); } public final void putValue(String key, long value) { editor.putLong(key, value); } public final void remove(String key) { editor.remove(key); } public final void commitEditor() { if (editor != null) editor.commit(); } public final void closeEditor() { editor=null; } }
这里用 Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_READABLE表示所有应用程序都对它可读可写。String spName="contacts"; int spMode=Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_READABLE; SharedPreferencesResolver spResolver=new SharedPreferencesResolver(Hello.this,spName,spMode); spResolver.OpenEditor(); spResolver.putValue("name", "hubin"); spResolver.putValue("phone", "13880890660"); spResolver.commitEditor(); spResolver.closeEditor(); Log.i(tag, spResolver.getValue("name", "")); Log.i(tag, spResolver.getValue("phone", ""));
注意需要在SharedPreferences创建时通过mode参数来设置非本地应用程序的访问权限。try { Context otherContext = this.createPackageContext("com.teleca", Context.CONTEXT_IGNORE_SECURITY); String name = "contacts"; int mode = Context.MODE_WORLD_WRITEABLE; SharedPreferences sp = otherContext .getSharedPreferences(name, mode); Log.i(tag, "name:" + sp.getString("name", "NA")); Log.i(tag, "phone:" + sp.getString("phone", "NA")); SharedPreferences.Editor editor = sp.edit(); editor.putString("wife", "yangyang"); editor.commit(); Log.i(tag, "wife:" + sp.getString("wife", "NA")); } catch (NameNotFoundException e) { Log.e(tag, "Erro:", e); }