Android development-save software settings-learn how to use SharedPreferences quickly-test

Source: Internet
Author: User

When designing software, we need to record the basic information of software settings. How can we save them? We can use SharedPreferences. SharedPreferences is an xml file used to store general settings of the software. For example, the user name in the login box or the user's own settings. SharedPreferences is automatically stored in data/data // shared_prefs

UserInfo. xml is the saved file. You can specify the file name. How can this problem be achieved? Next, let's take a step: Step 1: Create a SharedPreferences: android provides us with the or method. This method returns the SharedPreferences object when there is a file. No editor is executed after the Editor is created. commit () is created.
Original article:
Desired preferences file. if a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences. edit () and then commit changes (Editor. commit ()).
Code 1:
SharedPreferences preferences = getSharedPreferences("userInfo",Activity.MODE_PRIVATE);

Explanation: the first parameter of getSharedPreferences, userInfo, is the file name, And userInfo. xml is obtained. you can name it yourself.

The second parameter, Activity. MODE_PRIVATE, is the permission of the Activity when creating a file. Here is the full list of private permissions:

Context. MODE_PRIVATE = 0x0000
Context. MODE_WORLD_READABLE = 0x0001
Context. MODE_WORLD_WRITEABLE = 0x0002
Context. MODE_APPEND = 0x8000

Context. MODE_PRIVATE: the default operation mode. This 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, if you want to append the newly written content to the original file. You can use Context. MODE_APPEND.

Context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE are used to control whether other applications have the permission to read and write the file.

Context. MODE_APPEND: the mode checks whether the file exists and appends the content to the file if it exists. Otherwise, a new file is created.

MODE_WORLD_READABLE: indicates that the current file can be read by other applications; MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications.

If you want the file to be read and written by other applications, you can pass in:
OpenFileOutput ("leigo.txt", Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE );

 

Step 2: Initialize and add data:

Code 2:

// Obtain the editor information. This editor creates a map, which exists in the form of key-values. SharedPreferences. editor edit = preferences. edit (); // Add the value to the editor. edit. putBoolean ("isSendMsg", isSendMsg. isChecked (); edit. putBoolean ("isCall", isCall. isChecked (); edit. commit (); // note that you can create a preferences when no preferences exist.

  

After adding the file, we can view the stored value when opening the file. The file is stored in data/data // shared_prefs. If you do not know how to view the local mobile phone file, see android development-view and edit the sqlite database file-test.

Step 3: obtain the value in SharedPreferences and display it in the mobile phone.

SharedPreferences preferences = getSharedPreferences (PREFERENCES_NAME, Activity. MODE_PRIVATE); // obtain two controls for display. IsSendMsg = (Switch) findViewById (R. id. isSendMessage); isCall = (Switch) findViewById (R. id. isCall); // set preferences. getBoolean ("isSendMsg", true) is obtained and assigned to the control. The second parameter of getBoolean is, if preferences does not contain the default value. IsSendMsg. setChecked (preferences. getBoolean ("isSendMsg", true); isCall. setChecked (preferences. getBoolean ("isCall", false ));

  

Now, we have learned how to use SharedPreferences. Remember to save the information when you exit the program, which is generally used in the onStop () method.
@ Override protected void onStop () {super. onStop (); // automatically save the information SharedPreferences preferences = getSharedPreferences (PREFERENCES_NAME, Activity. MODE_PRIVATE); SharedPreferences. editor edit = preferences. edit (); edit. putBoolean ("isSendMsg", isSendMsg. isChecked (); edit. putBoolean ("isCall", isCall. isChecked (); edit. commit ();}

As a beginner in android, the first time I opened a blog, I would like to know more about the errors.

 

 

 

 

 

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.