Save one of Android data -- save Key-Values pair

Source: Internet
Author: User

You can use the SharedPreferences API to store key-value pairs, provide simple access methods, and control the access permissions of the SharedPreferences file. The following describes how to use the SharedPreferences API. Obtain a SharedPreferences object. Use the following method to obtain or create a SharedPreferences file object getSharedPreferences ()-Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. you can call this from anyContext in your app. getPreferences ()-Use this from an Activity if you need to use only one shared preference file for the activity. because this retrieves a default shared Preference file that belongs to the activity, you don't need to supply a name. for example, the following code is executed in a Fragment to obtain. string. the SharedPreferences file of the preference_file_key resource flag is opened in private mode, indicating that the file can only be accessed in your own app [java] Context context = getActivity (); SharedPreferences sharedPref = context. getSharedPreferences (getString (R. string. preference_file_key), Context. MODE_PRIVATE); when naming a SharedPreference file, it is best to use a unique name within the app range, such as com. ex Ample. myapp. PREFERENCE_FILE_KEY In addition, getPreferences () can get a SharedPreference file that only belongs to the current activity [java] SharedPreferences sharedPref = getActivity (). getPreferences (Context. MODE_PRIVATE); if it is enabled with MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE, other apps can also access the data to be written to the Shared Preferences file. First, create a SharedPreferences through edit. the Editor object is put into data through putInt (), putString (), and commit () is called to save the changes. For example, [java] SharedPreferences sharedPref = GetActivity (). getPreferences (Context. MODE_PRIVATE); SharedPreferences. editor editor = sharedPref. edit (); editor. putInt (getString (R. string. saved_high_score), newHighScore); editor. commit (); to read data from the Shared Preferences file, you can call methods such as getInt () and getString (), for example: [java] SharedPreferences sharedPref = getActivity (). getPreferences (Context. MODE_PRIVATE); long defaultVal = getResources (). getInteger (R. string. s Aved_high_score_default); long highScore = sharedPref. getInt (getString (R. string. saved_high_score), defaultVal); the second parameter indicates the default value when the provided key does not exist.

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.