Android data storage-SharedPreference

Source: Internet
Author: User

Android data storage-SharedPreference

As a complete application, data storage operations are essential. Therefore, the Android system provides four Data Storage Methods: SharedPreference, File, SQLite, and Content Provider. In the Android system, data is basically private and stored inData/package nameDirectory, all the data to be shared, the correct way to use Content Provider.

SQLite: SQLite is a lightweight database that supports basic SQL syntax and is a common data storage method. Android provides a type named SQLiteDatebase for this database, which encapsulates APIs for database operations.

SharedPreference: In addition to the SQLite database, another common data storage method is essentially an xml file, which is often used to store simple parameter settings.

File: The file (I/O) storage method is often used to store a large amount of data, but the disadvantage is that updating data will be difficult.

ContentProvider: A data storage method that can be shared by all applications in the Android system. Because data is usually private among applications, this storage method is rarely used, however, it is an essential storage method. For example, audio, video, image, and address book can be stored in a process. Each ContentProvider will provide a public URI (packaged as a Uri object) to the outside. If the application needs to share data, it needs to use ContentProvider to define a URI for the data, other applications then pass in this URI through the Content Provider to operate the data.

SharedPreference: It is a lightweight data storage method. It stores key-value pairs based on XML files and is usually used to save some simple configuration information. Its storage location isData/package name/shared_prefsDirectory. The SharedPreference object can only obtain data, but does not support storage and modification. The storage modification is implemented through the Editor object.

Compared with the SQLite database, the SharedPreference object removes the need to create databases, create tables, write SQL statements, and perform other operations, which is more convenient and concise. However, SharedPreference also has its own defects, such as its function to store five simple data types: boolean, int, float, long, and String. For example, SharedPreference cannot be used for conditional query. No matter how simple the SharedPreference data storage operation is, it can only be a supplement to the storage method, and cannot completely replace other data storage methods such as SQLite data.

SharedPreference storage operation procedure:

1. Get the SharedPreference object based on Context

 

Call the getSharedPreference () method of the Context object. The SharedPreference object obtained by this method can be shared by other components in the same application to call the getPreference () method of the Activity object, the SharedPreference object obtained by this method can only be used in this Activity. 2. Use edit () in the SharedPreference object () method to obtain the Editor object 3. Store key-value pairs in the Editor object 4. submit data using the commit () method of the Editor object
Four SharedPreference Operation Modes: Context. MODE_PRIVATE: The default operation mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file. Context. MODE_APPEND: This mode checks whether the file exists and appends content to the file if it exists. Otherwise, a new file is created. Context. MODE_WORLD_READABLE: Indicates that the current file can be read by other applications.
Context. MODE_WORLD_WRITEABLE: Indicates that the current file can be written by other applications.

 

Example: -- PassClick TextView to obtain the data saved by SharedPreference.

AndroidManifest. xml -- no changes have been made. The default value for creating a project is

 

Activity_main.xml

 

MainActivity. java

 

 

Package com. example. sharedpreferencedemo; import android. OS. bundle; import android. view. view; import android. widget. textView; import android. app. activity; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; public class MainActivity extends Activity {private TextView textView; private SharedPreferences sharedPreferences; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/* 1. Get the SharedPreference object. The saved file name */sharedPreferences = getSharedPreferences (hello, MODE_PRIVATE) is specified as "hello"./* 2. Store data, you can store multiple types of data. The data is marked with the key value */Editor editor = sharedPreferences. edit (); editor. putString (string, hello world .); editor. putInt (int, 10); editor. putBoolean (boolean, true);/* 3. submit data */editor. commit (); textView = (TextView) this. findViewById (R. id. textView); textView. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // if the key value does not exist, the data of the second parameter String = sharedPreferences is returned. getString (string,) ++ sharedPreferences. getInt (int, 0) ++ sharedPreferences. getBoolean (boolean, false); textView. setText (string); textView. setTextSize (20 );}});}}
Download program:

 

After the program is run, the data is saved in the/data/com. example. sharedpreferencedemo/shared_prefs/hello. xml file. Choose Window> Show View> Other> Androd> File Explorer.
View files


Click the first button in the upper-right corner of the File Explorer dialog box to export the hello. xml File from the receipt to your computer.

The content is as follows::

Program testing:--Click TextView to obtain the data saved by SharedPreference.


 

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.