Android Data Storage _sharedpreferences

Source: Internet
Author: User

Source Download (download points free): Download

Data collections for relatively few key-value pairs can be stored using sharedpreferences.
The data type of a key-value pair is basically a basic data type,
Common operations:

  • Create a new shared preference file or access the existing shared preference, 
    • getSharedPreferences () For the use of multiple sharedpreference files, you can use  
    • is used by the first parameter
    • getpreferences () You can use this when there is only one shared preference file,  
      This function returns the default shared preference file.
  • Write data to a shared preferences

    • In order to write the data into a shared preferences file, you need to call the edit () method to create
      Sharedpreferences.editor
    • And then call the appropriate write method to write the data,
    • Finally, the commit () method is called to save the changed data.
  • Reading data from a shared preferences
    In order to get data from the Sharred preferences file simply call the appropriate method such as Getint () or GetString ().

代码: 
@Override    protected voidOnresume() {        Super.Onresume();        //Gets the Sharedpreferences object.         sharedpreferencesprefs=getpreferences(0);        //Read data from a shared preference        StringStoredname=prefs.getString(USERNAME, NULL);        if (Storedname!= NULL) {EditText.SetText(Storedname);        }           }    @Override    protected voidOnPause() {        Super.OnPause();        //Get Sharedpreferences.editor object        sharedpreferences.EditorEditor=getpreferences(0).Edit();        //Storage dataEditor.putstring(USERNAME,EditText.GetText().toString());        //Save corresponding change Number dataEditor.Commit();    }
Note: When you use Mode_world_readable and mode_worle_writeable when you create a shared preference, other programs can access the data
References:http://developer.android.com/training/basics/data-storage/shared-preferences.html
http://developer.android.com/guide/topics/data/data-storage.html

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.