Sharedpreferences (1)-Basic Knowledge

Source: Internet
Author: User

When developing software, we often need to provide users with the software parameter setting function. For example, if we commonly use software, users can set whether to allow strangers to add themselves as friends. for saving software configuration parameters, if it is in the window, we usually use the INI file for saving. under J2EE, the properties attribute file or XML is used for saving. in our android applications, how can we save software configuration parameters? The Android platform provides us with a sharedpreferences class, which is a lightweight internal storage solution for lightweight applications. It is especially suitable for storing software configuration parameters, such as Boolean, Int, float, long, string and other data. use sharedpreferences to save data. The essence is to use an XML file to store data. The path is/data/<package name>/shared_prefs.

You can obtain sharedpreferences in either of the following ways:
1. Call the getsharedpreferences () method of the context object.
2. Call the getpreferences () method of the activity object.
Differences between the two methods:
The sharedpreferences object obtained by calling the getsharedpreferences () method of the context object can be shared by other components in the same application.
The sharedpreferences object obtained by calling the getpreferences () method of the activity object can only be used in this activity.


Sharedpreferences:
Context. mode_private
Context. mode_append

Context. mode_world_readable
Context. mode_world_writeable


Context. mode_private: the default operation mode. This mode indicates that the file is private data and can only be accessed by the application. In this mode, the written content will overwrite the content of the original 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.

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.

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.

Save the data to sharedpreferences:

SharedPreferences preferences=getSharedPreferences("user",Context.MODE_PRIVATE);Editor editor=preferences.edit();String name="xixi";String age="22";editor.putString("name", name);editor.putString("age", age);editor.commit();

Get data from sharedpreferences:

SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);String name=preferences.getString("name", "defaultname");String age=preferences.getString("age", "0");

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.