My Android Notes (11)--Save settings with preference

Source: Internet
Author: User

There are four ways to persist data in Android: SQLite database, file storage, Preference, ContentProvider.

Each of the four methods is specialized, and preference is stored in the form of a key-value pair like map, which is best used to store information such as the user's personal settings.

You can use an XML file to configure a setup interface and then display it with a dedicated preferenceactivity. Preferenceactivity is a professional set-up interface, as long as it is assigned a configured XML, it can automatically change the corresponding value of the program according to the Operation preference.

For example, in the Res directory to create an XML folder, and then create a new Seeting.xml file, the contents are as follows:

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <checkboxpreference
  4. android:defaultvalue="false"
  5. android:key="Boolean_value"
  6. android:summary="Save a Boolean value"
  7. android:title="Checkbox" />
  8. <edittextpreference
  9. android:defaultvalue="defaultstring"
  10. android:key="string_value"
  11. android:name="EditText"
  12. android:summary="Save a string"
  13. android:title="Edit Text" />
  14. </preferencescreen>

Checkboxpreference corresponds to a Boolean value, and edittextpreference corresponds to a string. Android:key is an identifier and must be unique.

Then create a settingactivity.java that reads as follows:

[Java]View Plaincopyprint?
  1. Import ANDROID.COM.UI.R;
  2. Import Android.os.Bundle;
  3. Import android.preference.PreferenceActivity;
  4. Public class Settingactivity extends preferenceactivity{
  5. @Override
  6. public void OnCreate (Bundle savedinstancestate) {
  7. super.oncreate (savedinstancestate);
  8. Addpreferencesfromresource (r.xml.setting);
  9. }
  10. }

Code Addpreferencesfromresource (r.xml.setting), which specifies an XML for this preferenceactivity, so that when this interface is opened:

Each time you change the settings of the interface, the program automatically persists to save the new value.

It is also very simple to get the values set in other places in the program, simply by using identifiers, such as getting the values of the identifiers "Boolean_value" and "string_value" above, just

[Java]View Plaincopyprint?
    1. Boolean booleanvalue = Preferencemanager.getdefaultsharedpreferences (
    2. This ). Getboolean ("Boolean_value", false);
    3. String stringvalue = preferencemanager.getdefaultsharedpreferences (this)
    4. . getString ("String_value", " " ");

can be obtained.

Preferencemanager.getdefaultsharedpreferences (Context) is a static method that acquires a global preference object that is unique anywhere in the program, The second parameter of the Getboolean and GetString methods is the default value, which is the default return value when the key fails to get.

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.