The following describes Android data storage. Android provides five data storage methods:
1. sharedpreferences stores data;
2. File storage data;
3. SQLite database stores data;
4. Use contentprovider to store data;
5. network storage data;
Today I will introduce the simplestSharedpreferences stores 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 regular long integer, int integer, and string storage 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. Android is stored in XML format at the end (in Android, the information is saved as an XML file in/Data/package_name/shared_prefsDirectory .), The overall efficiency is not particularly high. It is much better than SQLite for the conventional lightweight model. If the storage is really small, you can consider defining your own file format. During XML 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.
An interface used to access and modify the preference data returned by getsharedpreferences (string, INT. For any special preferences, all clients share one of these separate instances.
To modify preferences, you must use a sharedpreferences. Editor object to ensure that the preferences remain consistent when they submit data storage operations.
Note:
1. Currently, this class does not support multi-threaded access.
2. Using sharedpreferences has some limitations: it can only be used in the same package and cannot be used between different packages.
SharedpreferencesSteps:
1. Call edit () to get a sharedpreferences. Editor.
2. Add values with methods such as putboolean () and putstring ().
3. Commit the new values with commit ()
Code:
Public class sharedpreferencesactivity extends activity {private final static string setting = "potato"; private button mbtnsave; private button mbtnshow; private edittext meditviewname; private edittext meditviewage; private textview mtextviewresult; /** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mbtnsave = (button) findviewbyid (R. id. btnsave); mbtnshow = (button) findviewbyid (R. id. btnshow); meditviewname = (edittext) findviewbyid (R. id. editname); meditviewage = (edittext) findviewbyid (R. id. editage); mtextviewresult = (textview1_findviewbyid(r.id.txt result); mbtnsave. setonclicklistener (listener); mbtnshow. setonclicklistener (listener);}/*** event listener */private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {sharedpreferences pres = sharedpreferencesactivity. this. // Note 1 getsharedpreferences (setting, context. mode_private); Switch (v. GETID () {// save data case R. id. btnsave: Editor editor = Pres. edit (); // note 2editor. putstring ("name", meditviewname. gettext (). tostring (); Editor. putint ("Age", integer. valueof (meditviewage. gettext (). tostring (); Editor. commit (); toast. maketext (sharedpreferencesactivity. this, "saved successfully", toast. length_long ). show (); break; // display data case R. id. btnshow: string name = Pres. getstring ("name", "no"); // note 3int age = Pres. getint ("Age", 0); string Bir = Pres. getstring ("Bir", "no"); mtextviewresult. settext ("name =" + name + "; age =" + age + "; Bir =" + BIR); break ;}}};}
Note 1:
Sharedpreferences pres = sharedpreferencesactivity. This. // Note 1 getsharedpreferences (setting, context. mode_private );
Get a sharedpreferences by name. As the name suggests, this preferences is shared. The shared range is different from the package in Java, it seems that the package here refers to the package in androidmanifest. the package property value in the XML file.
Second parameter:
Context. mode_private: The default operation mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the newly written content to the original file. You can use context. mode_append.
Context. mode_append: The mode checks whether the file exists and appends the content to the file if it exists. Otherwise, a new file is created.
Context. mode_world_readable and context. mode_world_writeable are used to control whether other applications have the permission to read and write the file.
Mode_world_readable: Indicates that the current file can be read by other applications;
Mode_world_writeable: Indicates that the current file can be written by other applications.
NOTE 2:
NOTE 3:
The process of storage and value is a bit like hashmap, but it is more user-friendly than hashmap. getxxx (Object key, object defualtreturnvalue ),
The second parameter is the value returned when the key you want does not exist.(Default value ). This saves a lot of logical judgment ....
Editor. commit (); this must be called because the modified content can be submitted in this way. Otherwise, if a data type error occurs during the value assignment and an exception is thrown, the data will not be saved, but with commit (), it won't happen ....
If you have any questions, please leave a message .....