Android's Sharedpreferences detailed

Source: Internet
Author: User

Sharedpreferences is the most understandable data storage technology in Android, In fact, Sharedpreferences is dealing with a key-value (key-value pair) sharedpreferences commonly used to store some lightweight data.

1, the use of sharedpreferences to save data methods are as follows:

Instantiate the Sharedpreferences object (first step) sharedpreferences mysharedpreferences= getsharedpreferences ("Test", Activity.mode_ PRIVATE); Instantiate the Sharedpreferences.editor object (step two) Sharedpreferences.editor Editor = Mysharedpreferences.edit (); Save the data editor.putstring ("name", "Karl") with putstring method; Editor.putstring ("Habit", "sleep"); Submit Current Data editor.commit (); Use the TOAST information hint box to prompt for a successful write to data Toast.maketext (this, "data successfully written to sharedpreferences! ", Toast.length_long). Show ();
2, the use of Sharedpreferences read data method as follows:

3, sharedpreferences Four modes of operation: Context.MODE_PRIVATEContext.MODE_APPENDContext.MODE_WORLD_READABLEContext.MODE_ World_writeable context.mode_private: The default mode of operation, which means that the file is private data and can only be accessed by the app itself, in which the content written overwrites the contents of the original file Context.mode_ APPEND: The mode checks whether the file exists, appends content to the file, or creates a new file. Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the file. Mode_world_ Readable: Indicates that the current file can be read by another application. Mode_world_writeable: Indicates that the current file can be written by another application.

many times we develop software that requiresprovide software parameter setting function to user, such as our usual QQ, the user can set whether to allow strangers to add themselves as friends. For the preservation of software configuration parameters, if the window software is usually used to save the INI file, if it is a J2SE application, we will use the Properties attribute file or XML to save. If it's an Android app, what's the best way to save software configuration parameters? The Android platform provides us with a sharedpreferences class, which is a lightweight storage class that is especially suitable for saving software configuration parameters. Use Sharedpreferences to save data, behind it is to use XML files to store data, files stored in the/data/data/<package name>/shared_prefs directory:

Sharedpreferences sharedpreferences = getsharedpreferences ("LJQ", context.mode_private); Editor editor = Sharedpreferences.edit ();//Get editor editor.putstring ("name", "Lin Yi-chin"); Editor.putint ("Age", 24); Editor.commit ();//Submit Changes

the contents of the generated Ljq.xml file are as follows:

<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><map>   <string name= ' name ' > Lin Yi-chin </ string>   <int name= "age" value= "/></map>"

Since sharedpreferences is behind the use of XML files to save data,the first parameter of the Getsharedpreferences (Name,mode) method is used to specify the name of the file without a suffix. The suffix is automatically added by Android. The second parameter of the method specifies the mode of operation of the file, there are four modes of operation, which are described in the previous four modes of saving data using file mode. If you want the XML file behind sharedpreferences to be read and written by other applications, you can specify context.mode_world_readable and context.mode_world_writeable permissions.

In addition, the activity also provides another getpreferences (mode) method operation Sharedpreferences, which defaults to the name of the file using the current class without the package name.

accessing data in the Sharedpreferences

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, an application with <package name> for Com.ljq.action uses the following statement to create the preference.

Getsharedpreferences ("Ljq", context.mode_world_readable);

For other applications to access the preference applied above, you first need to create the context of the application above, and then access the preference through the context, accessing the preference when the app is under the package Shared_ Prefs Directory found preference:

Context Otherappscontext = Createpackagecontext ("Com.ljq.action", context.context_ignore_security); Sharedpreferences sharedpreferences = otherappscontext.getsharedpreferences ("LJQ", context.mode_world_readable); String name = sharedpreferences.getstring ("name", ""); int = Sharedpreferences.getint ("Age", 0);

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 by reading the XML file, such as:

File XMLFile = new file ("/data/data/<package name>/shared_prefs/itcast.xml");//<package name> should be replaced with the app's package name



Android's Sharedpreferences detailed

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.