One of data storage for Android development: haredpreferences [Source Code download is provided for free]

Source: Internet
Author: User
Data storage in Android development (1) [haredpreferences]

 

Most of the time, the software we develop needs to provide users with the software parameter setting function, such as our commonly used QQ, users can set whether to allow strangers to add themselves as friends. For the saving of software configuration parameters, if the window software is used, we usually use the INI file for saving. If it is a j2se application, we will use the properties property file for saving. For Android applications, how can we save software configuration parameters?

The Android platform provides us with a sharedpreferences class, which is a lightweight storage class and is especially suitable for storing software configuration parameters. Use sharedpreferences to save data. xml files are used to store data in the/data/<package name>/shared_prefs directory:

 

The following is a simple example:

We define two textviews and two edittexts in XML. The Code is as follows:

<Textview Android: Id = "@ + ID/view1" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "username"/> <edittextandroid: id = "@ + ID/userame" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <textview Android: Id = "@ + ID/view2" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "password"/> <edittextandroid: Id = "@ + ID/password" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: inputtype = "textpassword"/>

 

The code in sharedactivity. Java is as follows:

Private Static final string setting_infos = "setting_infos"; Private Static final string name = "name"; Private Static final string Password = "password"; private edittext username = NULL; private edittext Password = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); username = (edittext) findviewbyid (R. id. userame); Password = (edittext) findviewbyid (R. id. password); // get a sharedpreferences object sharedpreferences preferences = getsharedpreferences (setting_infos, 0); // read the content of the sharedpreferences storage file string namestring = preferences. getstring (name, ""); string Pwd = preferences. getstring (password, ""); username. settext (namestring); password. settext (PWD) ;}@ overrideprotected void onpause () {super. onpause (); // get the object. The first parameter is the file name for saving the XML file, and the second parameter is the operation mode for saving the file. sharedpreferences = getsharedpreferences (setting_infos, 0 ); // call edit to make it editable, write it in, and then submit sharedpreferences. edit (). putstring (name, username. gettext (). tostring ()). putstring (password, password. gettext (). tostring ()). commit () ;}//// @ override // protected void onstop () {// super. onstop (); // sharedpreferences = getsharedpreferences (setting_infos, 0); // sharedpreferences. edit ()//. putstring (name, username. gettext (). tostring ())//. putstring (password, password. gettext (). tostring ())//. commit ();////}


 

The content of the comment is written in onstop. It can be executed only when the program is completely exited and written in onpuse. It will be saved when you switch to another page.

For example, we enter data

Next, we will go to ddms, find the data under file explorer and find the folder named after our package name. The XML file under the pai_prefs folder in it is the file that saves the data, after export, use editplus to open it:

<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?>

<Map>

<String name = "password"> vvvvvghjnb </string>

<String name = "name"> dbkkk </string>

</Map>

Source code

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.