Android: Use of perferences

Source: Internet
Author: User

Browse the package directories in the/data/directory of the mobile phone. You will often see a shared_prefs folder withPackage name_preferences. xmlFile. This file is the main character;

This file is similar to the role of a configuration file and records some attribute values of the application. For example, if your application provides the wizard function to guide users, it is estimated that the option will be provided for the user to disable this function, so you can place this switch in this file, and the value will be correctly displayed at the next startup;

The operation of this configuration file mainly uses two classes: preferencemanager and sharedpreferences. sharedpreferences is used to perform specific operations on this configuration file, such as taking values from the file and writing values to the file; preferencemanager is responsible for managing the configuration files of all applications in the system. It can be used to easily obtain the sharedpreferences object of the file through the application context (content) and how to process the file path, the file names are all managed in a unified manner, so you don't need to worry about them when using them;

The following describes how to use it:

1. Import package

 
ImportAndroid. content. sharedpreferences;ImportAndroid. Preference. preferencemanager;

2. Get the object

Sharedpreferences mprefs = preferencemanager. getdefasharsharedpreferences (This);

3. Write/update

 
Sharedpreferences. Editor editor = mprefs. Edit (); editor. putboolean ("Pre_key_words",True); Editor. Commit ();

Of course, other types of data can be written here, such as putint and putstring. For details, refer to the methods in sharedpreferences. Java...

4. Values

BooleanCheckedkeywords = mprefs. getboolean ("Pre_key_words",False);

Note that"Pre_key_words"What values are set in the file, Boolean or string? Do not use the wrong function when setting the value; otherwise, the second parameter (default value) will always be returned;

 

In fact, sharedpreferencesPackage name_preferences. xmlReading and Writing files looks like reading and writing ini configuration files like VC ~

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.