Objective:
Recently has been learning iOS data storage, when learning to Nsuserdefaults reminds me of sharedpreferences, today idle to have no matter, think of summing up the use of sharedpreferences.
What is sharedpreferences storage?
Sharedpreferences is the base key-value (key-value pair) to store some light-weight data storage, especially for saving software configuration parameters. Using Sharedpreferences to save data, behind it is to use XML files to store data, files stored in/data/data/<package name>/shared_prefs.
How exactly is it used?
Write Data
Sharedpreferences sharedpreferences= getsharedpreferences ("Test", activity.mode_private); //instantiating a Sharedpreferences.editor objectSharedpreferences.editor Editor =Sharedpreferences.edit (); //Save the data in a putstring wayEditor.putboolean (User_islogin,true); Editor.putstring (user_id,"123456"); Editor.putstring (User_pwd,"pwd123"); //Submit Current DataEditor.apply ();
Reading data
Sharedpreferences sharedpreferences= getsharedpreferences ("Test", activity.mode_private); String userId=sharedpreferences.getstring (user_id,""); String userpwd=sharedpreferences.getstring (User_pwd,""); Boolean IsLogin=sharedpreferences.getboolean (User_islogin,false); LOG.E ("","userId---->"+userId); LOG.E ("","userpwd---->"+userpwd); LOG.E ("","islogin---->"+islogin);
Key Introduction under
Sharedpreferences sharedpreferences= getsharedpreferences ("test", Activity.mode_private);
The first parameter is the name of the file that generated the Xxx.xml file, and the second parameter is the file's access rights and how the file data is written. Briefly describe several ways:
activity.mode_private,//The default mode of operation, which means that the file is private and can only be accessed by the app itself, in which the contents of the original file are overwritten.
activity.mode_world_readable,//indicates that the current file can be read by another application.
activity.mode_world_writeable,//indicates that the current file can be written by another application;
Can be used in combination for example:
Look at the storage structure that writes the file specifically:
See the above stored data so clear is not frightened! Right! You're not mistaken! stored in the file is this, plaintext storage is not a bit of security, even if the set activity.mode_private permissions in front of the root authority is also vulnerable. So when we use sharedpreferences, we try not to store some data about the user information such as the userid,userpwd in this example, if you want to store how to remedy it? We can MD5 key for example MD5UTILS.MD5 (user_id), the value is encrypted, you can use AES encryption to attach a Androidaes encryption article (http://www.cnblogs.com/whoislcj/p/ 5473030.html).
Android Explore sharedpreferences data storage behind