SharedPreferences for android

Source: Internet
Author: User

SharedPreferences for android

This section describes how to store data in SharedPreferences to obtain the getSharedPreferences method in the Context class of the SharedPreferences object. Location: The getPreferences method in the Activity class PreferenceManager class shows how to read data from SharedPreferences.

Introduction

SharedPreferences Is One Of The Persistence Technologies in data storage solutions (others include file storage and database storage). It is a lightweight storage class and is especially suitable for saving software configuration parameters or saving temporary data. SharedPreferences is used to save data. behind it, xml files (key-value pairs) are used to store data, which is stored in the file system./data/data/ /shared_prefs Directory.

Store Data to SharedPreferences to obtain the SharedPreferences object.

To use SharedPreferences to save data, you must first obtain the SharedPreferences object. There are three methods:

GetSharedPreferences () method in the Context class

GetSharedPreferences () receives two parameters: the first parameter is used to specify the file name (if it does not exist, it is automatically created without a suffix, and the system automatically adds it), and the second parameter specifies the file operation mode, there are two main modes: MODE_PRIVATE and MODE_MULTI_PROCESS. MODE_PRIVATE has the same effect as directly passing in 0, indicating that only the current application can read and write the SharedPreferences file; MODE_MULTI_PROCESS is generally used when multiple processes read and write the same SharedPreferences file. In fact, there are two other types, but they have been deprecated after Android4.2. The simple code is as follows:

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(sywyg,0);

Note:

The SharedPreferences object obtained in this method can be stored as xml files by all Activity access files in the same application.

Open DDMS through the green android robot on the toolbar of android studio (it seems that DDMS is directly written in eclipse) to view the SD card File (File Explorer). My SharedPreferences File is stored in: /data/com. sywyg. intent_test/shared_prefs/Name: sywyg. xml. The exported xml file is as follows:


      
   
    sywyg
       
   

We can see that SharedPreferences stores data through key-value pairs.

GetPreferences () method in Activity Class

The second method is to obtain the SharedPreferences object: The getPreferences () method in the Activity class. This method only accepts one mode parameter, and the file name is automatically the Class Name of the current Activity. The simple code is as follows:

SharedPreferences sharedPreferences2 = this.getPreferences(0);

The storage location of the SharedPreferences file created in this way remains unchanged, while the xml name is MainActivity. xml. The content is as follows:


      
   
    sywyg
       
   

Note:

This method is used to obtain the SharedPreferences object. If other activities want to obtain the SharedPreferences file, they can only get the SharedPreferences object through the getSharedPreferences () method in the Context class. Of course, the file name must be the SharedPreferences File. Getdefasharsharedpreferences () method in the PreferenceManager class

This method is a static method with only one Context parameter and automatically uses the package name of the current application as the prefix to name the SharedPreferences file. The simple code is as follows:

SharedPreferences sharedPreferences3 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

The location of the SharedPreferences file created in this way remains unchanged, while the xml name is com. sywyg. intent_test_preferences.xml. The content is as follows:


      
   
    sywyg
       
   
Save data to the SharedPreferences File

After obtaining the SharedPreferences object, you can save data to the SharedPreferences file in the following three steps:

Call the edit () method of the SharedPreferences object to obtain a SharedPreferences. editor (internal class) object; Use the putXXX () method to add data to the Editor object; call the commit () method to submit data to complete data storage operations.

Note:

Generally, data is stored in the onPause () method.

The Code is as follows:

@Overrideprotected void onPause(){   super.onPause();   SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(sywyg,0);   //SharedPreferences sharedPreferences = this.getPreferences(0);   //SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());   SharedPreferences.Editor editor = sharedPreferences.edit();   editor.putString(name,sywyg);   editor.putLong(id,1);   editor.commit();}

Note:

The data types saved through the putXXX () method can only be String, int, boolean, long, float; or a String set. Read data from SharedPreferences

The corresponding data can be obtained through getXXX () of the SharedPreferences object. The getXXX () method receives two parameters. The first parameter is the key (the key name used for storing data ), the second parameter is the default value, that is, this value is returned when the corresponding key is not found.

The Code is as follows:

@ Overrideprotected void onResume () {super. onResume (); SharedPreferences sharedPreferences = getApplicationContext (). getSharedPreferences (sywyg, 0); String name = sharedPreferences. getString (name,); long id = sharedPreferences. getLong (id, 0); Toast. makeText (this, name: + name +, id: + id, Toast. LENGTH_LONG ). show (); SharedPreferences. editor editor = sharedPreferences. edit (); // clear the Content editor in the SharedPreferences file. clear (); editor. commit ();}
Result demo

 

Related Article

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.