Android Explore sharedpreferences data storage behind

Source: Internet
Author: User

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

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.