I: firstapp)The data code in sharedpreferences is as follows:
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://ypf3027.javaeye.com/blog/812812# ">
- Sharedpreferences = getsharedpreferences ("first_app_perferences", context. mode_private );
- String name = sharedpreferences. getstring ("name", ""); // The second parameter getstring () is the default value. If this key does not exist in preference, the default value is returned.
- IntAge = sharedpreferences. getint ("Age", 1 );
Sharedpreferences = getsharedpreferences ("first_app_perferences", context. mode_private); string name = sharedpreferences. getstring ("name", ""); // getstring () The second parameter is the default value. If the key does not exist in the preference, the default value int age = sharedpreferences is returned. getint ("Age", 1 );
II: accessing other applicationsPreference (access firstapp data in secondapp), provided that the context. mode_world_readable or context. mode_world_writeable permission is specified when the preference of firstapp is created.
For example, the preference ("first_app_perferences") is created in <package name> for an application of COM. First. app using the following statement ").
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://ypf3027.javaeye.com/blog/812812# ">
- Getsharedpreferences ("first_app_perferences", context. mode_world_readable );
Getsharedpreferences ("first_app_perferences", context. mode_world_readable );
To access preference in the firstapp in secondapp, you must first create the context of the firstapp and then access preference through context. when accessing preference, the preference will be found in the shared_prefs directory of the app's package:
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://ypf3027.javaeye.com/blog/812812# ">
- Context firstappcontext = createpackagecontext ("com. First. app", context. context_ignore_security );
- Sharedpreferences = firstappcontext. getsharedpreferences ("first_app_perferences", context. mode_world_readable );
- String name = sharedpreferences. getstring ("name ","");
- IntAge = sharedpreferences. getint ("Age", 0 );
Context firstappcontext = createpackagecontext ("com. first. APP ", context. context_ignore_security); sharedpreferences = firstappcontext. getsharedpreferences ("first_app_perferences", context. mode_world_readable); string name = sharedpreferences. getstring ("name", ""); int age = sharedpreferences. getint ("Age", 0 );
If you do not create a context to access the preference of the firstapp, you can directly access the XML file of the preference of the firstapp by reading the XML file, for example:
File xmlfile = new file ("/data/<package name>/shared_prefs/first_app_perferences.xml"); // <package name> should be replaced with the package name of the application: COM. first. app