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.

Package Org. 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 extends preferenceactivity implements attributes {private edittextpreference address; private edittextpreference port; private edittextpreference group; private listpreference type; private checkboxpreference keepalive; private checkboxpreference ack; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // Add the preferences layout file. The method of adding the preferences to the general layout file is different. Note the difference between addpreferencesfromresource (R. layout. preference); // get a shared preferences. 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 the configuration parameters. Parameter 1 is the preferred parameter, which is generally in the configuration file of Chengdu, parameter 2 is the default parameter. If parameter 1 cannot be read, use parameter 2 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);} @ overridepublic Boolean onpreferencechange (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:

<? 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.