android--Getting Sharedtpreference instances

Source: Internet
Author: User

Now I know that there are three common ways to get a sharedpreferences instance:

1. Through the getsharedpreferences of the context (String Name,int mode).

2. Getpreferences(int mode) through activity.

3. Getdefaultsharedpreferences (context context) through the static method of the Sharedpreferencemanager.


The value and meaning of mode:

mode is specified as Mode_private, the profile can only be accessed by its own application.
mode is specified as mode_world_readable, the configuration file can be read by other programs, in addition to its own access.
mode is specified as mode_world_writeable, the configuration file can be read and written by other programs, in addition to its own access



Three different ways to differentiate:


The getsharedpreferences (String name,int mode) of the context specifies the filepermissions for the file name and the specified pattern according to our specified name.


Source code in activity:


From the source code of the activity we can see that we invoke the Getpreference method in the activity to actually call the context getsharedpreferences (String name,int mode) method. The only difference is that it will follow the pattern we specify and the name of the file using the current class without the package name.



Sharedpreferencemanager in the source code:


Source code We can see that we call Getdefaultsharedpreference (context context) This method is actually called the context of the getsharedpreferences (String name, int mode) method, except that the XML file we generated is named PackageName plus _preferences, and the file will be in the default mode_private mode.


Summarize:

The essential difference is that the names of the files are different and the access rights are not the same. The first name and permission are designed by yourself. The second only permission is our design, the file name is the current class without the package name of the class name . The third name is the package name plus _preferences, which defaults to access only to its own application.



android--Getting Sharedtpreference instances

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.