@ will keep on adding
1. Initialization of Sharedpreferences objects
Three different ways:
1) getsharedpreferences (String name, int mode)
2) Method of Preferencemanager getsharedpreferences ()
3) Getdefaultsharedpreferences method
The 23rd is to use the system's default XML as a file to store data sharedpreferences.
The first is an XML file that you create yourself to store data.
The difference is that the first one is relatively more widely used. The two or three method can be used to generate a cache that stores the entire app state.
2. Sharedpreference.editor's apply and Commit methods
Accidental IDE reminds you to use apply in a scene better.
The difference between the two methods is:
1) apply does not return a value and a commit returns a Boolean indicating whether the change was committed successfully
2) Apply the content submitted to the memory, and then submit to the XML, wherein there are two instances of the process is submitted, the former is covered by the latter case,
Commit is committed directly to the XML, when the occurrence of two instances of the simultaneous submission of the situation, will be processed synchronously. As a result, the commit thread is more secure, but at the expense of speed.
3) But the general sharedpreferences is a single instance, generally there is no concurrency conflict, so using apply will be faster.
3, Sharedpreference general application scenario:
A, check the user is not the first time to login
B, application last update time check
C, save user login user name
D. Save the state of the application
E. Location information for cached users
4, a good way to use Sharedpreference: Sharedpreferences + singleton = cache
privateParentCache(Context context){ parentPreferences = PreferenceManager.getDefaultSharedPreferences(context); } publicstaticgetInstance(Context context) { ifnull) { new ParentCache(context.getApplicationContext()); } return parentCache; } publicvoidclearCache() { parentPreferences.edit().clear().apply(); }
5. How to access preference in other applications (copy)
If you access preference in another app, you have specified context.mode_world_readable or context.mode_world_writeable permissions when the preference was created. For example, there is an app for cn.yang.action that uses the following statement to create a preference.
Getsharedpreferences ("TEST", context.mode_world_readable);
1) Other applications to access the above applied preference, first need to create the context of the application above, and then through the context to access the preference, access preference when the app is under the package Shared_ Prefs Directory found 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", "");
2) If you do not access another app's preference by creating a context, you can also directly access other applications preference the corresponding XML file in the form of a read XML file, such as:
File XMLFile = new file ("/data/data//shared_prefs/itcast.xml");//should be replaced with the app's package name
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sharedpreferences's experience in use