Use sharedpreferences for Android data storage and access

Source: Internet
Author: User

Most of the time, the software we develop needs to provide users with the software parameter setting function, such as our commonly used QQ, users can set whether to allow strangers to add themselves as friends. For saving software configuration parameters, if the window software is used, we usually use the INI file for saving. If it is a j2se application, we will use the properties property file or XML for saving. For Android applications, how can we save software configuration parameters? The Android platform provides us with a sharedpreferences class, which is a lightweight storage class and is especially suitable for storing software configuration parameters. Sharedpreferences is used to save data. xml files are used to store data in/data/<package
Name>/shared_prefs directory:

Sharedpreferences = getsharedpreferences ("Config", context. mode_private );
Editor editor = sharedpreferences. Edit (); // get the editor
Editor. putstring ("name", "Laotian ");
Editor. putint ("Age", 4 );

Editor. Commit (); // submit the modification

The generated config. xml file contains the following content:
<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?>
<Map>
<String name = "name"> Laotian </string>
<Int name = "Age" value = "4"/>
</Map>
Because sharedpreferences uses an XML file to save data, the first parameter of the getsharedpreferences (name, mode) method is used to specify the name and name of the file.No suffix requiredThe suffix is automatically added by Android. The second parameter of the method specifies the file operation mode. There are four operation modes. the preceding four modes are described when you use the File mode to save data. If you want the XML file used by sharedpreferences to be read and written by other applications, you can specify the context. mode_world_readable and context. mode_world_writeable permissions.
In addition, activity provides another getpreferences (mode) method to operate sharedpreferences. By default, This method uses the Class Name of the current class without the package name as the file name.

The data code used to access sharedpreferences is as follows:
Sharedpreferences = getsharedpreferences ("itcast", context. mode_private );
// The second parameter of getstring () isDefault ValueIf this key does not exist in preference,Returns the default value

String name = sharedpreferences. getstring ("name ","");
Int age = sharedpreferences. getint ("Age", 1 );

If preferenceThe precondition is that the context. mode_world_readable or context. mode_world_writeable permission is specified when the preference is created. For example, an application whose <package name> is CN. itcast. action creates a preference using the following statement.
Getsharedpreferences ("itcast", context. mode_world_readable );
Other applications need to access the preference of the above applicationFirst, you need to create the context of the above application, and then access preference through context. When you access preference, the preference will be found in the shared_prefs directory of the application's package:

Context othersponcontext = createpackagecontext ("com. OCE. Action", context. context_ignore_security );
Sharedpreferences = otherequcontext. getsharedpreferences ("Config", 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 other applications, you can also directly access the XML file corresponding to the preference of other applications by reading the XML file, for example:
File xmlfile = new file ("/data/<package name>/shared_prefs/itcast. xml"); // replace <package name> with the package name of the application

Related Article

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.