Detailed usage SharedPreferences

SharedPreferencesIs Androida 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, intand so on. Will generally complex types of data into Base64encoded, stored in the form of a string. SharedPreferencesBehind is xmlto store data files, files are stored in /data/data/<package name>/shared_prefsthe directory.
Use SharedPreferencessaved key-valuesteps are as follows:

  1. Using Activityclass getSharedPreferencesmethod to obtain SharedPreferencesthe object, wherein the stored key-valuefile name from the getSharedPreferencesfirst argument is specified.
  2. Use SharedPreferencesinterface editto obtain SharedPreferences.Editorthe object.
  3. Through the SharedPreferences.Editorinterface putXxxmethod for storing key-valuepairs which Xxxrepresent different types of data. For example: string type valuerequired by putStringthe method.
  4. By SharedPreferences.Editorinterface commitpreservation methods key-valueright, committhe method is equivalent to submit operational database transaction.

Use SharedPreferencessaved key-valuefor 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.xmlcontent 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 SharedPreferencesread 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);   

SharedPreferencesThe four modes of operation:

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
Context.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.

Use SharedPreferencesread by other applications Preference:
read by other applications Preferencepreconditions are: The preferencetime specified is created Context.MODE_WORLD_READABLEor Context.MODE_WORLD_WRITEABLEpermission.
For example: There <package name>is com.xiuxiuing.testan 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 Contextaccess preference, access preferencewhen the application will be located under the package shared_prefsto 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.EditorThere are applyand committwo methods for submitting data editing, the difference between these two methods is that:

  1. applyNo return value and commitreturns booleanindicate whether the submission is successful modification

  2. applyIs to modify the data atomic committed to memory, and then submitted to truly asynchronous hardware disk, and commitis committed to the disk hardware synchronization, therefore, in multiple concurrent submission commitwhen they are dealing with waits commitsaved to disk in operation, thereby reducing the efficiency. And applyjust to submit atoms content, followed by calls applywill function directly covering the front of the memory data, thereby improving the lot of efficiency to some extent.

  3. applyThe method does not prompt any prompt failure.

Since a process, sharedPreferencea 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 committhe.

No personal welcome attention to the public

Published 115 original articles · won praise 67 · Views 100,000 +

Guess you like

Origin blog.csdn.net/meifannao789456/article/details/100079205