SharedPreferences是一种轻量级的数据存储方式,它可以用键值对的方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/包名/shared_prefs/)自己定义的xml文件中。
以下表格为获取SharedPreferences对象的两个方法:
返回值 |
函数 |
备注 |
SharedPreferences |
Context.getSharedPreferences(String name,int mode) |
name为本组件的配置文件名(如果想要与本应用程序的其他组件共享此配置文件,可以用这个名字来检索到这个配置文件)。 mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。 |
SharedPreferences |
Activity.getPreferences(int mode) |
配置文件仅可以被调用的Activity使用。 mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。 |
如果要读取配置文件信息,只需要直接使用SharedPreferences对象的getXXX()方法即可,而如果要写入配置信息,则必须先调用SharedPreferences对象的edit()方法,使其处于可编辑状态,然后再调用putXXX()方法写入配置信息,最后调用commit()方法提交更改后的配置文件。
We can save preferences in three ways:
To save preferences that are accessed only from a single activity, we do it like this:
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor=prefs.edit(); editor.putString("pref 1", "some text"); editor.commit();
We get a SharedPreferences object by calling getPreferences(int mode) method which takes an integer value as a parameter, the mode value can be one of the following:
Then we get an instance of SharedPreferences.Editor and write the preference value with editor.putString(String key, String value) method.
shared preferences allows you to insert preferences using the following methods:
Then we call edit.commit() to save the preferences to the file. commit returns a boolean indicating the result of saving, true if successful and false if failed.
To read preferences values:
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE); String val=prefs.getString("pref 1", "some text");
We use sharedpreferences.getString(String key, String defaultValue) (or get boolean/float/int) to return the value stored with a specific key or defaultValue if not found.
To save preferences that can be retrieved from all activities in the application we do it like this:
SharedPreferences prefs= getSharedPreferences("demopref", Context.MODE_WORLD_READABLE); SharedPreferences.Editor editor=prefs.edit(); editor.putString("demostring", "hello"); editor.commit();
Same as the code above, but the difference is that we give our preferences file a name (demopref in this case) so that other activities can reference that preferences file.
We can store preferences in one application and read them in another application, this is done reading the preferences file by loading them through the first application’s context.
Let’s assume we have two applications:
If application1 creates a preferences file with the name “demopref” and inserts a String preference with the key/value “demostring/hello”.
now we access this file and value from application 2 like this:
Context con; try { con = createPackageContext("com.minasamy.prefdemo", 0); SharedPreferences pref=con.getSharedPreferences("demopref", Context.MODE_PRIVATE); String x=pref.getString("demostring", "not found"); txt.setText(x); } catch (NameNotFoundException e) { Log.e(Tag, e.toString()); }
在一个应用程序里,需要访问另外一个应用程序的 SharePreferences,在我们的应用程序里可以这样进行:
Context context=null;
try {}
SharedPreferences settings=context.getSharedPreferences("musicEQ", 3);//3表示是即可读又可写,即MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE的值
boolean isSystemWallperSettings=settings.getBoolean("musicEQ", false);//获取另外一个应用程序的SharedPreferences的值。