SharedPreferences
Is Android
a lightweight storage class platform provides us with a very simple to use, easy to store data and read data, particularly suitable for storing software configuration parameters. SharedPreferences can only save simple data types, such as: String
, int
and so on. Will generally complex types of data into Base64
encoded, stored in the form of a string. SharedPreferences
Behind is xml
to store data files, files are stored in /data/data/<package name>/shared_prefs
the directory.
Use SharedPreferences
saved key-value
steps are as follows:
- Using
Activity
classgetSharedPreferences
method to obtainSharedPreferences
the object, wherein the storedkey-value
file name from thegetSharedPreferences
first argument is specified. - Use
SharedPreferences
interfaceedit
to obtainSharedPreferences.Editor
the object. - Through the
SharedPreferences.Editor
interfaceputXxx
method for storingkey-value
pairs whichXxx
represent different types of data. For example: string typevalue
required byputString
the method. - By
SharedPreferences.Editor
interfacecommit
preservation methodskey-value
right,commit
the method is equivalent to submit operational database transaction.
Use SharedPreferences
saved key-value
for the following code:
SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();//获取编辑器
editor.putString("name", "小明");
editor.putInt("age", 24);
editor.commit();//提交修改
Generated test.xml
content files as follows:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">小明</string>
<int name="age" value="24" />
</map>
Using the SharedPreferences
read data codes are as follows:
SharedPreferences sharedPreferences= getSharedPreferences("test",
Activity.MODE_PRIVATE);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString("name", "");
int age =sharedPreferences.getInt("age", 0);
SharedPreferences
The four modes of operation:
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
Context.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
Use SharedPreferences
read by other applications Preference
:
read by other applications Preference
preconditions are: The preference
time specified is created Context.MODE_WORLD_READABLE
or Context.MODE_WORLD_WRITEABLE
permission.
For example: There <package name>
is com.xiuxiuing.test
an application created using the following statementpreference
getSharedPreferences("test", Context.MODE_WORLD_READABLE);
We want to access the above applications preference
, you first need to create the above application Context
, and then Context
access preference
, access preference
when the application will be located under the package shared_prefs
to find the directory preference
, as follows:
Context context = createPackageContext("com.xiuxiuing.test", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = context.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 0);
Description:
SharedPreferences.Editor
There are apply
and commit
two methods for submitting data editing, the difference between these two methods is that:
-
apply
No return value andcommit
returnsboolean
indicate whether the submission is successful modification -
apply
Is to modify the data atomic committed to memory, and then submitted to truly asynchronous hardware disk, andcommit
is committed to the disk hardware synchronization, therefore, in multiple concurrent submissioncommit
when they are dealing with waitscommit
saved to disk in operation, thereby reducing the efficiency. Andapply
just to submit atoms content, followed by callsapply
will function directly covering the front of the memory data, thereby improving the lot of efficiency to some extent. -
apply
The method does not prompt any prompt failure.
Since a process, sharedPreference
a single-instance, generally does not appear concurrency conflicts, if the result of the author do not care, it is recommended to use apply
, of course, the need to ensure a successful submission and follow-up operation, then it needs to use commit
the.