Android Getsharedpreferences and Getpreferences

Source: Internet
Author: User

Today, the getsharedpreferences and getpreferences are used for the preservation of small programming parameters.

First, the calls to Getsharedpreferences and Getpreferences are modularized and made into a class. The key points to use for these two functions are given in the code as comments.

Class is implemented as follows:

1  Packagecom.hs.leozheng.backuprecords; 2   3 Importandroid.app.Activity; 4 ImportAndroid.content.Context; 5 Importandroid.content.SharedPreferences; 6   7 /* 8 * Two sets of functions are defined, using Getsharedpreferences and getpreferences respectively9  * */  Ten    One  Public classConfigsdata { A     //Public static final String netconfigsdata = "Netconfigsdata";  -      Public Static FinalString NETCONFIGIP = "NETCONFIGIP";  -        the     /*  - * You can create a new Shared preferences file or access an existing file by using one of the following two methods - 1, getsharedpreferences () If there are multiple Shared Preferences files in the application that need to be saved, this method is suitable for - The first parameter is the ID you specified for this file, which can be invoked by the context of the application. + Getsharedpreferences is a method in the Context class that can specify the filename and mode - 2, Getpreferences () if the application only needs to save a Shared Preferences file, this method is good for + because there is only one Shared Preferences file applied to it, you do not need to specify a name for it, and the system defaults to a name when it is created A Getpreferences is a method in the Activity class, just specify mode at      * */   -        -     /*  - * According to the API document description, Sharedpreferences is a singleton class, so it is easy to ensure synchronization of multi-party simultaneous operation - * Sharedpreferences actually manages the XML file, all the settings are stored in the Key-value file in the/data/data/package_name/shared_prefs directory. -      * */   in     BooleanSaveconfigdataex (Context context,string configname,string value) { -Sharedpreferences settings = context.getsharedpreferences (configname, 0);  toSharedpreferences.editor Editor =Settings.edit ();  + editor.putstring (configname, value);  - Editor.commit ();  the            *         return true;  $     }  Panax Notoginseng        - String Readconfigdataex (Context context,string configname) { theSharedpreferences settings = context.getsharedpreferences (configname, 0);  +String IP = settings.getstring (configname, "127.0.0.1");  A            the         returnIP;  +     }   -        $     /* ---------------------------------------------------------------------------------- */   $     Booleansaveconfigdata (Activity activity,string configname,string value) { -Sharedpreferences settings =activity.getpreferences (context.mode_private);  -Sharedpreferences.editor Editor =Settings.edit ();  the editor.putstring (configname, value);  - Editor.commit (); Wuyi            the         return true;  -     }   Wu        - String readconfigdata (Activity activity,string configname) { AboutSharedpreferences settings =activity.getpreferences (context.mode_private);  $String IP = settings.getstring (configname, "127.0.0.1");  -            -         returnIP;  -     }   A}

Class is implemented, a simple test of the class is necessary. The following test code is written, first parsing the two functions that call getsharedpreferences and getpreferences, followed by calls to both functions.

As shown in the following code:

1 voidTestcallconfigsave (activity activity) {2Configsdata Configsdata =NewConfigsdata (); 3String Netip =configsdata.readconfigdata (activity, CONFIGSDATA.NETCONFIGIP); 4LOG.I (Phonebookinfo.tag, "Read config Data,ip is:" +Netip); 5       6Configsdata.saveconfigdata (activity, CONFIGSDATA.NETCONFIGIP, "192.168.3.9"); 7Netip =configsdata.readconfigdata (activity, CONFIGSDATA.NETCONFIGIP); 8LOG.I (Phonebookinfo.tag, "Read config Data,ip is:" +Netip); 9       TenConfigsdata.saveconfigdata (activity, CONFIGSDATA.NETCONFIGIP, "192.168.3.10");  OneNetip =configsdata.readconfigdata (activity, CONFIGSDATA.NETCONFIGIP);  ALOG.I (Phonebookinfo.tag, "Read config Data,ip is:" +Netip);  - }   -    the voidTestcallconfigsaveex (Context context) { -Configsdata Configsdata =NewConfigsdata ();  - Configsdata.readconfigdataex (context, CONFIGSDATA.NETCONFIGIP);  -String Netip =Configsdata.readconfigdataex (context, CONFIGSDATA.NETCONFIGIP);  +LOG.I (Phonebookinfo.tag, "Readex config Data,ip is:" +Netip);  -        +Configsdata.saveconfigdataex (context, CONFIGSDATA.NETCONFIGIP, "192.168.4.9");  ANetip =Configsdata.readconfigdataex (context, CONFIGSDATA.NETCONFIGIP);  atLOG.I (Phonebookinfo.tag, "Readex config Data,ip is:" +Netip);  -        -Configsdata.saveconfigdataex (context, CONFIGSDATA.NETCONFIGIP, "192.168.4.10");  -Netip =Configsdata.readconfigdataex (context, CONFIGSDATA.NETCONFIGIP);  -LOG.I (Phonebookinfo.tag, "Readex config Data,ip is:" +Netip);  -}

Called in the Activity's onCreate ( note the different parameters ):

1 testcallconfigsave (this);   2

The LOG output after two runs is as follows:

The LOG for the first run is as follows (it is the first time to run after uninstalling):   read config data,ip is:127.0.0.1   read config data,ip is:192.168.3.9   read Config D Ata,ip is:192.168.3.10   readex config data,ip is:127.0.0.1   readex config data,ip is:192.168.4.9 readex   conf    the second run of the IG Data,ip is:192.168.4.10 LOG is as follows:   read config data,ip is:192.168.3.10   read config Data,ip is:192.16 8.3.9   Read config data,ip is:192.168.3.10   readex config data,ip is:192.168.4.10   readex config Data,ip is : 192.168.4.9   readex config data,ip is:192.168.4.10  

At this point, the test is complete. The code called for Getsharedpreferences and getpreferences functions correctly.

Android Getsharedpreferences and Getpreferences

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.