Android data storage method (2)-shared preferences

Source: Internet
Author: User

There are four data storage methods for Android:

1. Shared preferences

It is mainly used to store data in key-value pairs. It is a lightweight storage mechanism and can only store basic data types.

2. Files

Fileinputstream and fileoutputstream are used to operate files. Because files in Android are an applicationProgramPrivate, so other applications cannot read or write.

3. SQLite

The standard database provided by Android supports SQL statements. For more information about this part, see my other article.Article: Http://www.cnblogs.com/wenjiang/archive/2013/05/28/3100860.html

4. Network

Store and obtain data through the network.

Shared preferences is introduced here.

Shared preferences is mainly used to save system configuration information, for example, checkbox. We want to save the previous user's choice when we start the application next time. The interface of the Android system is in the form of an activity stack, so some interfaces will be recycled when the system resources are insufficient, which may cause the loss of our interface information. Using shared preferences allows us to save attributes of an application just like using an INI file. These attributes are user-defined modifications or custom parameter values.

We use a small example to illustrate the use of shared preferences:

We only use one checkbox to save the user's married information.

The interface is very simple:

 <  Relativelayout  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  Xmlns: Tools  = "Http://schemas.android.com/tools"  Android: layout_width = "Match_parent"  Android: layout_height  = "Match_parent"  Android: paddingbottom  = "@ Dimen/activity_vertical_margin"  Android: paddingleft  = "@ Dimen/activity_horizontal_margin"  Android: paddingright  = "@ Dimen/activity_horizontal_margin"  Android: paddingtop  = "@ Dimen/activity_vertical_margin"  Tools: Context  = ". Mainactivity"  >      <  Checkbox  Android: ID  = "@ + ID/checkbox"  Android: layout_width  = "Wrap_content"  Android: layout_height  = "Wrap_content"  Android: checked  = "False"  Android: Text  = "@ String/choice"   />  </ Relativelayout  > 

Next let's take a look at mainactivity:

 Public   Class Mainactivity Extends  Activity {  Private   Boolean  Ischeck; @ override  Protected   Void  Oncreate (bundle savedinstancestate ){  Super . Oncreate (savedinstancestate); setcontentview (R. layout. activity_main); checkbox = (Checkbox) This  . Findviewbyid (R. Id. checkbox); sharedpreferences setting = Getpreferences (activity. mode_private); ischeck = Setting. getboolean ("check ", False  ); Checkbox. setchecked (ischeck); checkbox. setoncheckedchangelistener (  New  Oncheckedchangelistener () {@ override  Public   Void Oncheckedchanged (compoundbutton buttonview,  Boolean  Ischecked) {ischeck = Ischecked ;}});}  Public   Boolean Onkeydown ( Int  Keycode, keyevent event ){  If (Keycode = Keyevent. keycode_back) {sharedpreferences preference = Getpreferences (0 ); Sharedpreferences. Editor =Preference. Edit (); editor. putboolean ( "Check" , Ischeck); editor. Commit ();  This  . Finish ();  Return   True  ;}  Return   Super  . Onkeydown (keycode, event) ;}@ override  Public   Boolean  Oncreateoptionsmenu (menu ){ //  Inflate the menu; this adds items to the action bar if it is present.  Getmenuinflater (). Inflate (R. Menu. Main, menu );  Return   True  ;}} 

We can use the getpreferences () method to obtain the preferences object. Generally, our preferences will not be shared with other programs, because this is the configuration information of the program. If it can be read and written by other programs, the consequences are very serious. Perform operations on the information saved by preferences, such as adding the object sharedpreferences. Editor. Then, save the corresponding key-value pairs and submit them.
Now we can exit and restart the application, and we will find that the original selection is still saved.

If you want to know where the data is stored, open ddms, View File explorer, and view the shared_prefs folder in/data in the corresponding package, the XML file is the file that saves the data.

all the data stored by the application is stored in this folder.

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.