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 suited for saving software configuration parameters such as Boolean,int, float,long,string data. Using Sharedpreferences to save data, the essence is to use the 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 to differentiate: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 invoking the Getpreferences () method of the activity object can only be used in that activity.
(3) the Sharedpreferences () property, which contains four modes.
3.1, Context.mode_private: The default mode of operation, the representative file is a private file, can only be accessed by the application itself, in which the file will overwrite the original file content
3.2, Context.mode_append: This mode will check whether the file exists, the presence of the file to append content, or create 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) Save the data to Sharedpreferences
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");














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.