Use sharedpreferences for data storage in Android

Source: Internet
Author: User

The sharedpreferences provided by Android is similar to WindowsProgramCommon INI files are saved in the form of key-value (data type differentiation ). For example, it can be used to save the user's last login information, the volume setting of the Media Player and the last playback position.

Frequently used methods when using sharedpreferences include ):

    1. Context. getsharedpreferences (string name, int Mode)

    2. Activity. getpreferences (INT Mode)

    3. Sharedpreferences. Edit ();

    4. Sharedpreferences. getxxx (name, defaultvalue );

    5. Sharedpreferences. Editor. putxxx (name, value );
    6. Sharedpreferences. Editor. Commit ();

Steps:

Save data

Log. D ("sharedpreferences", "save data ...");
//Get sharedpreferences object
Sharedpreferences settings =This. Getsharedpreferences ("shared_file", 0 );
//Get editable object
Sharedpreferences. Editor editor = settings. Edit ();
Editor. putstring ("name", "Kael Chen ");
Editor. putint ("Age", 22 );
Editor. Commit ();
Log. D ("sharedpreferences", "data saved successfully ");

Read data

Log. D ("sharedpreferences", "Get data ...");
Sharedpreferences settings =This. Getsharedpreferences ("shared_file", 0 );
String name = settings. getstring ("name", "No Name ");
IntAge = settings. getint ("Age", 0 );
Log. D ("sharedpreferences", "Name:" + name + ", age:" + age );
Log. D ("sharedpreferences", "data retrieved ");

RunCodeYou can see that a shared_file.xml file is added to the data/pacakge name/shared_prefs/directory. The file name is determined by the parameters passed in by the getsharedpreferences method.

The file content is as follows:

 <?  XML version = '1. 0' encoding = 'utf-8' standalone = 'yes'  ?> 
< Map >
< String Name = "Name" > Kael Chen </ String >
< Int Name = "Age" Value = "22" />
</ Map >

We can see that, unlike the INI file, the data type is strictly distinguished here, so the type used when getxxx and putxxx are the same must match.

In addition, it is worth noting that the mode parameter in the getsharedpreferences method andGetpreferencesAnd getsharedpreferences.

GetpreferencesDifference from getsharedpreferences:

1. getpreferencesIt can only be called by activity and is only valid within the scope of activity that calls it.

2. getsharedpreferences are called by context and valid within the package range. As you can understand, androidmanifest. the XML file defines the location of the package. For example, the package defined in my test program is CN. kael. sample, then android will establish data/CN. kael. sample/shared_prefs/directory under which the sharepreferences file is generated. Therefore, if you have used sharedprefernces in one place and created the corresponding sharedprefernces file, and then you want to use the sharedprefernces file in another place, you only need to determine whether the path generated by using sharedprefernces in the new place is consistent with the path of the previous sharedprefernces File Based on the file path rules to know whether the two can share sharedprefernces, even if they do not belong to the same application (because Android decides the creation location of the sharedprefernces File Based on the package name, it is irrelevant to the Application name ).

About the mode parameter:

To access the sharedprefernces file of program a in program B:

First, program A needs to use mode_world_writeable or mode_world_readable as the mode parameter when creating sharedprefernces:

 
Sharedpreferences settings =This. Getsharedpreferences ("shared_file", context. mode_world_writeable );

Then, in program B, you need to first create the context object of program A, and then call the getsharedpreferences method through this context object:

Context contexta = createpackagecontext ("cn. Kael. sample ",
Context. context_ignore_security );
Sharedpreferences settings = contexta. getsharedpreferences (
"Shared_file", context. mode_world_writeable );

However, what I don't understand is that whether this mode is set to mode_world_writeable or mode_world_readable, you can read and write the sharedprefernces file of program a in program B, the changes to the sharedprefernces file after reading and writing are only valid in program B. In addition, the following error message appears in logcat:

 
16:52:00-30. 965: Error/applicationcontext (25124): Couldn't rename file/data/CN. kael. sample/shared_prefs/shared_file.xml to backup file/data/CN. kael. sample/shared_prefs/shared_file.xml.bak

Please tell me the reason. Thank you !!!

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.