Android storage-sharedpreference storage (user preference storage)

Source: Internet
Author: User
Tags xml parser

Data storage is most frequently used during development. Here we mainly introduce five Data Storage Methods on the Android platform:
1. Use sharedpreferences to store data;
2. File storage data;
3. The SQLite database stores data;
4. Use contentprovider to store data;
5. network storage data;

Sharedpreferences is a lightweight storage class on the Android platform. It mainly stores some common configurations, such as window status. Generally, reload the window status onsaveinstancestate in the activity and save it using sharedpreferences, it provides the storage of long integer, int integer, and string types on the Android platform.

 

What kind of processing method is it? Sharedpreferences is similar to the ini configuration file on windows in the past, but it is divided into multiple permissions and can be shared globally. android123 prompts that the file is saved in XML format. The overall efficiency is not particularly high, the lightweight architecture is much better than SQLite. If the storage volume is small, you can define your own file format. XML
During Processing, Dalvik will parse the local XML parser that comes with the underlying layer, such as the xmlpull method, so that the memory resource usage is better.

 

 
In essence, it stores key-value pair data based on XML files and is usually used to store some simple configuration information.

is stored in the/data/ /shared_prefs directory.

The sharedpreferences object can only obtain data, but does not support storage and modification. The storage modification is implemented through the editor object.

JavaCode:

Package CN. jason. store; import Java. text. simpledateformat; import Java. util. date; import android. app. activity; import android. content. sharedpreferences; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class sharepreferencewriteactivity extends activity {sharedpreferences preferences; sharedpreferences. editor editor; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // get can only be used by this applicationProgram Read and Write sharedpreferences object preferences = getsharedpreferences ("11", mode_world_readable); // get the modifier editor = preferences. edit (); button READ = (button) findviewbyid (R. id. read); button write = (button) findviewbyid (R. id. write); read. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {int num = preferences. getint ("num", 0); string a = num + ""; // use toast to indicate toast. maketext (sharepreferencewriteactivity. this, A, 5000 ). show () ;}}); write. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {// store a random number editor. putint ("num", (INT) (math. random () * 100); // submit all stored data editor. commit ();}});}}

The focus of user storage sharedpreference is:

 
// Get the sharedpreferences object preferences = getsharedpreferences ("11", mode_world_readable) that can only be read and written by this application; // get the modifier editor = preferences. Edit ();

In the above Code. Getsharedpreferences ("11", mode_world_readable); the two parameter values are: the first parameter is the User-Defined name, and the second parameter is the read permission.

The above three are commonly used permissions. The first one is only accessible to the program, the second and third are accessible to other programs, but only the read or write permissions.

The following program accesses the sharedpreference of the above program through other programs.

Package CN. jason. io; import android. app. activity; import android. content. context; import android. content. sharedpreferences; import android. content. PM. packagemanager. namenotfoundexception; import android. OS. bundle; import android. widget. textview; public class readotherpreferenceactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Context usecount = NULL; try {usecount = createpackagecontext ("CN. jason. store ", context. context_ignore_security);} catch (namenotfoundexception e) {e. printstacktrace ();} sharedpreferences preferences = usecount. getsharedpreferences ("Jason", context. mode_world_readable); // read data int num = preferences. getint ("num", 0); textview show = (textview) findviewbyid (R. id. show); // display the read data content show. settext ("" + num );}}

The key of the above Code is to first establish the context at the reporting level, and then read the sharedpreferences of other packages.


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.