Use sharedpreferences to store and read data

Source: Internet
Author: User

tag: Ar CTI Div for Android using Io file data

1. When storing the software we develop, we need to provide users with the software parameter setting function. For example, if we use QQ, users can set whether to allow strangers to add themselves as friends. For saving software configuration parameters, if the window software is used, we usually use the INI file for saving. If it is a j2se application, we will use the properties property file or XML for saving. For Android applications, how can we save software configuration parameters? The Android platform provides us with a sharedpreferences class, which is a lightweight storage class and is especially suitable for storing software configuration parameters. Use sharedpreferences to save data. xml files are used to store data in the/data/<package name>/shared_prefs Directory: sharedpreferences = getsharedpreferences ("itcast ", context. mode_private); Editor editor = sharedpreferences. edit (); // get the editor. putstring ("name", "Chuanzhi podcast"); Editor. putint ("Age", 4); Editor. commit (); // submit the modified generated itcast. the content of the XML file is as follows: <? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Map> <string name = "name"> Chuanzhi podcast </string> <int name = "Age" value = "4"/> </map> because sharedpreferences are used XML file to save data, the first parameter of the getsharedpreferences (name, mode) method is used to specify the name of the file without a suffix. The suffix is automatically added by Android. The second parameter of the method specifies the file operation mode. There are four operation modes. the preceding four modes are described when you use the File mode to save data. If you want the XML file used by sharedpreferences to be read and written by other applications, you can specify the context. mode_world_readable and context. mode_world_writeable permissions. In addition, activity provides another getpreferences (mode) method to operate sharedpreferences. By default, This method uses the Class Name of the current class without the package name as the file name. 2. Read the data code used to access sharedpreferences: sharedpreferences = getsharedpreferences ("itcast", context. mode_private); // getstring () The second parameter is the default value. If the key does not exist in the preference, the default value string name = sharedpreferences is returned. getstring ("name", ""); int age = sharedpreferences. getint ("Age", 1); If you access preference from other applications, the precondition is that context is specified when the preference is created. mode_world_readable or context. mode_world_writeable permission. For example, an application whose <package name> is CN. itcast. action creates a preference using the following statement. Getsharedpreferences ("itcast", context. Mode_world_readable ); To access the preference of the above application, other applications must first create the context of the above application and then access the preference through the context. when accessing the preference, the preference will be found in the shared_prefs directory under the package where the application is located: context othersponcontext = createpackagecontext ("CN. itcast. action ", context. Context_ignore_security ); Sharedpreferences = other1_context. getsharedpreferences ("itcast", context. mode_world_readable); string name = sharedpreferences. getstring ("name", ""); int age = sharedpreferences. getint ("Age", 0); if you do not create a context to access preference of other applications, you can also directly access the XML file corresponding to preference of other applications by reading the XML file, such: file xmlfile = new file ("/data/<package name>/shared_prefs/itcast. XML); // <package name> should be replaced with the application package name l

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.