Access Data in sharedpreferences of other programs

Source: Internet
Author: User

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# ">
  1. Sharedpreferences = getsharedpreferences ("first_app_perferences", context. mode_private );
  2. 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.
  3. 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# ">
  1. 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# ">
  1. Context firstappcontext = createpackagecontext ("com. First. app", context. context_ignore_security );
  2. Sharedpreferences = firstappcontext. getsharedpreferences ("first_app_perferences", context. mode_world_readable );
  3. String name = sharedpreferences. getstring ("name ","");
  4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.