Specific explanations for sharedpreferences functions in Android

Source: Internet
Author: User

The Android platform provides a sharedpreferences class, which is a lightweight storage solution inside a lightweight application that is especially suitable for saving software configuration parameters, such as Boolean,int, float,long,string data. Using Sharedpreferences to save data, in fact, the use of XML file storage data, the path is:/data/data/<package name>/shared_prefs .There are two ways to get sharedpreferences:
(1) Call the Getsharedpreferences () of the context object;(2) Call the Activity object's getpreferences ();
two different ways:The Sharedpreferences object obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application.

The Sharedpreferences object obtained by the


(3) Sharedpreferences () property, including four modes.
 3.1, Context.mode_private: The default mode of operation, representing the file is a private file, can only be used by the application itself to ask. Writing to a file in this mode overwrites the original file contents
 3.2, Context.mode_append: This mode checks whether the file exists, appends content to the file, or creates a new file.
 3.3, context.mode_world_readable: Indicates that the current file can be read by another application.
 3.4, context.mode_world_writeable: Indicates that the current file can be written by another application.

(4)

Sharedpreferences preferences=getsharedpreferences ("user", context.mode_private); Editor Editor=preferences.edit (); String name= "Xixi"; String age= "editor.putstring" ("Name", "name"), Editor.putstring ("age", age); Editor.commit ();

(5)getting data from Sharedpreferences
Sharedpreferences preferences=getsharedpreferences ("user", context.mode_private); String name=preferences.getstring ("name", "DefaultName"); String age=preferences.getstring ("Age", "0");














Specific explanations for sharedpreferences functions in Android

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.