Home>

When browsing the directories of each package in the/data/data/directory of the mobile phone, you often see a shared_prefs folder with a package name _preferences.xml file.Hereinafter referred to as the configuration file;

This file is similar to the role of a configuration file,Record some attribute values ​​of the application,For example, if your app provides a wizard function that guides users,Then I'm sure it will provide an option for users to turn off this feature.Then you can place this switch in this file,The correct display can be made by this value at the next startup;

The operation of this configuration file mainly uses two classes:preferencemanager and sharedpreferences. Sharedpreferences is used to specifically operate the configuration file.If you take a value from a file,Write values ​​to a file, etc .;preferencemanager is responsible for managing the configuration files of all applications of the system,You can use it to easily get the sharedpreferences object of the file through the context of the application, how to handle the file path,He has managed all the file names,Don't care when using it;

Here's how to use it specifically:

1.Import package

import android.content.sharedpreferences;
import android.preference.preferencemanager;

2.Get the object

sharedpreferences mprefs=preferencemanager.getdefaultsharedpreferences (this);

Write/Update

sharedpreferences.editor editor=mprefs.edit ();
editor.putboolean ("pre_key_words", true);
editor.commit ();

Of course, other types of data can also be written here,Such as putint, putstring, you can specifically view the methods in sharedpreferences.java ...

4.Value

boolean checkedkeywords=mprefs.getboolean ("pre_key_words", false);

To be careful of,Pay attention to what value "pre_key_words" is set in the file itself,Is it boolean or string?Do not use the wrong function when taking values,Otherwise, it always returns the second parameter (the default value);

In fact, sharedpreferences reads and writes the package name_preferences.xml file,It looks similar to reading and writing ini configuration files such as vc ~

  • Previous Introduce Jquery data (), Jquery stop (), jquery delay () functions one by one (details)
  • Next Very comprehensive summary of PHP datetime operations