Android: Storage mode sharepreferences

Source: Internet
Author: User

Using Sharedpreferences to save data, the essence is to use the XML file to store data,

Storage location:/data/data/<package name>/shared_prefs

Write:

    Public voidwritedata (view view) {//instantiate the Sharedpreferences object, parameter 1 is the name of the stored file, parameter 2 is the way the file is opened, and when the file does not exist, it is created directly, and if it exists, it is used directlySharedpreferences mysharepreferences =getsharedpreferences ("test", activity.mode_private); //instantiating a Sharedpreferences.editor objectSharedpreferences.editor Editor =Mysharepreferences.edit (); //Save the data in a putstring wayEditor.putstring ("name", "tinyphp"); //Submit DataEditor.commit (); //Prompt SuccessToast.maketext ( This, "Data successfully written", Toast.length_short). Show (); }

Mode is default to 0 or mode_private
Mode_private, read only, accessible only by its own application
Mode_world_readable, readable, in addition to their own access can also be read by other programs should be
Mode_world_writeable, can be written, in addition to their own access can also be read by other programs and write

Read:

 Public void readdata (view view) {        // Instantiate Sharedpreferences object        sharedpreferences Myshareperferences =getsharedpreferences ("Test", activity.mode_private);                 // Get values        with GetString String name =myshareperferences.getstring ("name", "");                 // Show        Toast.maketext (This, "read data \ n" + "name:\n" +name, Toast.length_short). Show ()                  ;

Android: Storage mode sharepreferences

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.