Introduction to SharedPreferences of Android, android. uid. shared

Source: Internet
Author: User

Introduction to SharedPreferences of Android, android. uid. shared
Introduction to SharedPreferences for Android I. Introduction to SharedPreferences

SharedPreferences is a lightweight data storage method. It stores key-value pair data based on XML files and is usually used to store some simple configuration information. Its storage location is in the/data/<package name>/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. Typical usage methods include user input box storage for past login accounts. To store SharedPreferences, follow these steps:

1. Get the SharedPreferences object based on Context
2. Use the edit () method to obtain the Editor object.
3. Use the Editor object to store key-value pairs.
4. submit data using the commit () method. Ii. Relationship between SharedPreferences and Editor 1. SharedPreferences

Public abstract SharedPreferences getSharedPreferences (String name, int mode)

Method to obtain a sharedpreferences object. The parameter name is the name of the preference file, and the mode is the method. The default value is 0.

2. Editor

Editor can be used to add, delete, modify, and query SharedPreferences data.

Public abstract SharedPreferences. Editor putString (String key, String value)

By executing the commit () or apply () method, the changes will be applied.

Iii. SharedPreferences code snippet

 

// Store sharedpreferencespublic void setSharedPreference () {sharedPreferences = getSharedPreferences ("itcast", Context. MODE_PRIVATE); Editor editor = sharedPreferences. edit (); editor. putString ("username", text1.getText (). toString (); editor. putInt ("password", getpw (); editor. commit (); // submit changes} // clear the public void removeSharedPreference () {sharedpreferences = getSharedPreferences ("itcast", Context. MODE_PRIVATE); Editor editor = sharedPreferences. edit (); editor. remove ("username"); editor. remove ("password"); editor. commit (); // submit changes} // obtain the public void getSahrePreference () {String username = sharedpreferences of sharedPreferences. getString ("username", ""); int password = sharedPreferences. getInt ("password", 0); String str = String. valueOf (password); text1.setText (username); text2.setText (str );}


Demo address: http://download.csdn.net/detail/stop_pig/7885113


How to Use SharedPreferences in services in android

Data transmission between two activities can be passed through intent, and SharedPreferences can also be used to share data.
SharedPreferences is easy to use.
In
Set
Editor uses data = getSharedPreferences ("data", 0). edit ();
Using data. putString ("item", "hello getSharedPreferences ");
Using data. commit ();
Obtain from B
Java code
SharedPreferences upload data = getSharedPreferences ("data", 0 );
String data = response data. getString ("item", null );
Log. v ("cola", "data =" + data );

Sharedpreferences of Android

Deleting an xml file cannot completely delete SharedPreferences. For details, refer to the android source code. This item is static. As long as it is not recycled, the original data can still be read in the cache.
You can delete the file by using the clear editor and using the commit command. It cannot be so violent ............

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.