It provides the Android platform General Long shaping, int shaping, string string type of save, what is it the processing method? Sharedpreferences similar to the previous INI configuration file on a Windows system, but it is divided into multiple permissions, you can share access globally, android123 hints are ultimately stored in XML, the overall efficiency is not particularly high, It's a lot better for conventional lightweight than SQLite, and if it's really storage, it's not a good concept to define the file format. XML processing is Dalvik through the native XML parser, such as the Xmlpull method, which is good for memory resource occupancy.
This should be the easiest way to use Android to read and write external data. His usage is basically the same as in J2SE (Java.util.prefs.Preferences), in a simple, transparent way to save some user personalization font, color, location, and other parameter information. A typical application would provide such an interface as "settings" or "preferences", and the settings would eventually be saved by preferences, and the programmer would not need to know exactly what form it saved and where it was stored. Of course, if you are willing to save other things, there is no limit. I just don't know what the problem is with the performance.
The steps for implementing sharedpreferences storage are as follows:
First, get the Sharedpreferences object according to the context
Second, use the edit () method to obtain the editor object.
Store Key-value key value pairs through the editor object.
Submission of data through the commit () method.
the writing process for the specific code is:
A, storing data information
1, open preferences, the name is setting, if present, open it, otherwise create a new preferences
| The code is as follows |
Copy Code |
Sharedpreferences settings = getsharedpreferences ("setting", 0); |
2, let setting in the editing state
| The code is as follows |
Copy Code |
Sharedpreferences.editor Editor = Settings.edit (); |
3. Storing data
| The code is as follows |
Copy Code |
Editor.putstring ("name", "Ataaw"); Editor.putstring ("URL", "ataaw.com"); |
4, complete the submission
| The code is as follows |
Copy Code |
Editor.commit (); |
B, read the data information
1. Get Preferences
| The code is as follows |
Copy Code |
Sharedpreferences settings = getsharedpreferences ("setting", 0); |
2, take out the data
| The code is as follows |
Copy Code |
String name = settings.getstring ("name", "Default value"); String url = setting.getstring ("url", "Default"); |
This is how sharedpreferences is used in Android, where the preferences file location can be viewed in eclipse:
| The code is as follows |
Copy Code |
Ddms->file Explorer/<package Name>/shared_prefs/setting.xml |
Cases
| The code is as follows |
Copy Code |
| public class Mainactivity extends activity { @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
Get Sharedpreferences Object Context ctx = Mainactivity.this; Sharedpreferences sp = ctx.getsharedpreferences ("sp", mode_private); Depositing data Editor Editor = Sp.edit (); Editor.putstring ("String_key", "STRING"); Editor.putint ("Int_key", 0); Editor.putboolean ("Boolean_key", true); Editor.commit ();
Returns the value of the String_key LOG.D ("SP", Sp.getstring ("String_key", "none")); If Not_exist does not exist, the return value is "none" LOG.D ("SP", Sp.getstring ("Not_exist", "none")); } } |
The specific contents of the Sp.xml document are as follows:
| code is as follows |
copy code |
| <?xml Version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes ' >2 <map>3 <string name= ' String_key ' String>4 <int name= "Int_key" value= "0"/>5 <boolean name= "Boolean_key" value= "true"/>6 </map> |