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