The sharedpreference of "Android Sunflower Treasure Book"

Source: Internet
Author: User

There are four basic methods of data preservation in Android, one is Sharedpreference, two is file, three is SQLite, four is contentprovider. Sharedpreferences is widely used as a simple and simple way to save configuration information for your application.

Sharedpreferences is a way to store configuration information for an application in key-value pairs , which can only store basic data types. A program's configuration file can only be used in this application, or can only be used within the same package, and cannot be used between different packages . In fact Sharedpreferences is the use of XML format to store data in the device, in Ddms in the file Explorer /data/data/<package name>/shares_prefs the next.


writing data to Sharedpreferences

Java code
  1. Sharedpreferences settings = getsharedpreferences (Prefs_name, 0);
  2. Sharedpreferences.editor Editor = Settings.edit ();
  3. Editor.putboolean ("Silentmode", Msilentmode);
  4. Editor.commit (); //Be sure to remember to submit

reading data from the Sharedpreferences

Java code
  1. Sharedpreferences settings = getsharedpreferences (Prefs_name, 0);
  2. //or using sharedpreferences settings = getpreferences (0);
  3. Boolean silent = Settings.getboolean ("Silentmode", false);

Function Description:

1, android.content.Context.getSharedPreferences (String name, int mode)

Returns the contents of the Preferences file ' name '. There is only one sharedpreferences for a name, so any modifications can be understood to take effect. The name Preferences file name MODE defaults to 0 (mode_private) to indicate that the preferences file is private to the app, and only this app can accessmode_world_readableIndicates that all other programs have Read permissionsmode_world_writeableIndicates that all other programs have write permissions
2, android.app.Activity.getPreferences (int mode)Returns the currentactivity-Private sharedpreferences. Equivalent to the above getsharedpreferences (String, int) method name passed in as the class name of the current activity

3, Android.preference.Preference.getSharedPreferences ()

Returns the sharedpreferences that the current preference can access


The sharedpreference of "Android Sunflower Treasure Book"

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.