Android advanced tutorial (7)-use preferences in Android!

Source: Internet
Author: User

Hello everyone, this section describes how to learn Android preferences. preferences are used in Android to record applications and user preferences. They can be used to save

Simple data types, such as int, double, and Boolean. The data stored in preferences can be understood as map type. We passPreferencemanager

AndGetdefasharsharedpreferences (context)

For example, if we want to obtain an integer, we can use

getInt(String key, int defVal)


. Get a key value in it. When we want to modify it, we use

putInt(String key, int newVal),


Last use

edit(),



Method submission! Never forget it ~

 

In order to make everyone better understand, I made a simple demo. The program mainly has a textview control, which says the number of times the user used to change the application. Shows the effect:

 


 

Below are the general steps for implementing the demo:

 

1. Create an android project named preferencesdemo.

 

2. Modify the main. xml layout file. Here, only an ID is added to the textview control. The Code is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/text" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> </linearlayout> <br/>

 

3. Modify the preferencedemo. Java code. The Code is as follows:

 

Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. content. sharedpreferences; <br/> Import android. OS. bundle; <br/> Import android. preference. preferencemanager; <br/> Import android. widget. textview; <br/> public class preferencesdemo extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> sharedpreferences mperferences = preferencemanager <br/>. getdefasharsharedpreferences (this); </P> <p> int counter = mperferences. getint ("counter", 0); </P> <p> textview mtextview = (textview) findviewbyid (R. id. text); </P> <p> mtextview. settext ("this app has been started" + counter + "times. "); </P> <p> sharedpreferences. editor meditor = mperferences. edit (); </P> <p> meditor. putint ("counter", ++ counter); <br/> meditor. commit (); </P> <p >}< br/>}

 

4. Run the code to achieve the above effect.

 

5. view the preferences file. First open the command terminal: ADB shell, and then run CD Data/data to enter the directory. After ls, we will find a lot of package files, as shown in:

 


 

CD com. Android. Tutor

(Here is the package name of my program)

/Shared_prefs, ls will find the. xml file, for example:

 


 

Open the. xml file in the following format (for your understanding ):

 

<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Br/> <map> <br/> <int name = "counter" value = "3"/> <br/> </map> <br/>

 

OK. This is the end of today. All of the above are my ignorance. If anything is wrong, please correct me. Thank you!

 



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.