Usage of SharedPreferences in android

Source: Internet
Author: User

 

In android Application Development, preferences, which are used as the configuration environment, are widely used. I will introduce the usage of SharedPreferences in android in detail. Let's take a look at the effect of the configuration screen.

Is the preferences page.

Click set IP address to pop up the Setting Dialog Box:

 

 

 

To facilitate SharedPreferences management, android provides a simple and quick PreferenceActivity class. by inheriting this class, you can implement various beautiful preferences UI settings. The following shows the java code of the preferences page. The class name is MyPreferenceActivity. java.

 

 

View plain

Package org. Volume UE;

Import android. content. SharedPreferences;

Import android. OS. Bundle;

Import android. preference. CheckBoxPreference;

Import android. preference. EditTextPreference;

Import android. preference. ListPreference;

Import android. preference. Preference;

Import android. preference. Preference. OnPreferenceChangeListener;

Import android. preference. PreferenceActivity;

/**

* Preference usage example

* @ Author xionglei

*

*/

Public class MyPreferenceActivity extends PreferenceActivity implements OnPreferenceChangeListener {

 

Private EditTextPreference address;

Private EditTextPreference port;

Private EditTextPreference group;

Private ListPreference type;

Private CheckBoxPreference keepAlive;

Private CheckBoxPreference ack;

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

// Add the preferences layout file, which is different from the common layout file. Note the difference.

AddPreferencesFromResource (R. layout. preference );

// Obtain a shared preferences and use this variable to read the parameter values in the configuration file

SharedPreferences sp = getPreferenceManager (). getSharedPreferences ();

Address = (EditTextPreference) findPreference ("prefer_address ");

Address. setOnPreferenceChangeListener (this );

Port = (EditTextPreference) findPreference ("prefer_port ");

Port. setOnPreferenceChangeListener (this );

Group = (EditTextPreference) findPreference ("prefer_ht_group ");

Group. setOnPreferenceChangeListener (this );

Ack = (CheckBoxPreference) findPreference ("prefer_sendACK ");

Ack. setOnPreferenceChangeListener (this );

Type = (ListPreference) findPreference ("prefer_request_type ");

Type. setOnPreferenceChangeListener (this );

KeepAlive = (CheckBoxPreference) findPreference ("prefer_keepAlive ");

KeepAlive. setOnPreferenceChangeListener (this );

// The getString method of sp has two parameters. This method is used to obtain configuration parameters. Parameter 1 is the preferred parameter. Generally, in the Chengdu configuration file, parameter 2 is the default parameter, if parameter 1 cannot be read, parameter 2 is used.

String typeStr = sp. getString ("prefer_request_type", getResources (). getString (R. string. type ));

Type. setSummary (typeStr );

Type. setValue (typeStr );

String addressStr = sp. getString ("prefer_address", getResources (). getString (R. string. ip ));

Address. setSummary (addressStr );

Address. setText (addressStr );

String portStr = sp. getString ("prefer_port", getResources (). getString (R. string. p ));

Port. setSummary (portStr );

Port. setText (portStr );

String groupStr = sp. getString ("prefer_ht_group", getResources (). getString (R. string. group ));

Group. setSummary (groupStr );

Group. setText (groupStr );

Ack. setChecked (sp. getBoolean ("prefer_sendACK", Boolean. getBoolean (getResources (). getString (R. string. alive ))));

KeepAlive. setChecked (sp. getBoolean ("prefer_keepAlive", Boolean. getBoolean (getResources (). getString (R. string. alive ))));

}

 

@ Override

Public boolean onPreferenceChange (Preference preference, Object newValue ){

If (preference = address ){

Preference. setSummary (newValue. toString ());

}

If (preference = port ){

Preference. setSummary (newValue. toString ());

}

If (preference = group ){

Preference. setSummary (newValue. toString ());

}

If (preference = type ){

Type. setSummary (newValue. toString ());

}

If (preference = ack ){

Preference. setSummary (newValue. toString ());

}

If (preference = keepAlive ){

Preference. setSummary (newValue. toString ());

}

Return true;

}

}

 

The layout file of preferences is as follows:

 

 

 

View plain

<? Xml version = "1.0" encoding = "UTF-8"?>

<PreferenceScreen

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: title = "Settings">

<PreferenceCategory

Android: title = "Settings">

<EditTextPreference

Android: key = "prefer_address"

Android: title = "@ string/setAddress"/>

<EditTextPreference

Android: key = "prefer_port"

Android: numeric = "integer"

Android: title = "@ string/port"

/>

<ListPreference

Android: key = "prefer_request_type"

Android: title = "@ string/requsttype"

Android: dialogTitle = "@ string/requsttype"/>

<CheckBoxPreference

Android: key = "prefer_keepAlive"

Android: title = "@ string/keep_alive"

Android: summary = "false"

/>

<CheckBoxPreference

Android: key = "prefer_sendACK"

Android: title = "@ string/send_ack"

Android: summary = "false"

/>

<EditTextPreference

Android: key = "prefer_ht_group"

Android: title = "@ string/ht_group"

Android: dialogTitle = "@ string/ht_group"/>

</PreferenceCategory>

 

</PreferenceScreen>

 

 

 

The layout file format is android standard.

After setting the MyPreferenceActivity class, you can obtain system parameters through sharedPreference on other activity pages. The generated xml configuration file is located in the data/org/UE/shared_prefs directory. You can use pull to view the data.

 

Well, I will explain it here today. If you still have some questions, you can download the source code to study it carefully,

Source Code address: http://download.csdn.net/detail/bearray123/3722067

Reprinted please indicate the source http://blog.csdn.net/bearray123

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.