There are two ways to get Sharedpreferences: 1 call the Getsharedpreferences () method of the context object to call the Getpreferences () method of the activity object in two ways: The Sharedpreferences object that is obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application. Invoke the Activity object's getpreferences () The Sharedpreferences object obtained by the method can only be used in this activity. sharedpreferences Four modes of operation: Context.MODE_PRIVATEContext.MODE_ Appendcontext.mode_world_readablecontext.mode_world_writeable context.mode_private: The default mode of operation, which represents the file as private data, can only be accessed by the app itself, in which the contents of the write overwrite the contents of the original file Context.mode_append: The schema checks whether the file exists, appends content to the file, or creates a new file. Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the file. Mode_world_ Readable: Indicates that the current file can be read by another application. Mode_world_writeable: Indicates that the current file can be written by another application. Save data to Sharedpreferences:sharedpreferences Preferences=getsharedpreferences ("User", context.mode_private); Editor Editor=preferences.edit (); String name= "Xixi"; String age= "editor.putstring" ("Name", "name"), Editor.putstring ("age", age), Editor.commit (); Get data from Sharedpreferences: Sharedpreferences preferences=getsharedpreferences ("User", context.mode_pRivate); String name=preferences.getstring ("name", "DefaultName"); String age=preferences.getstring ("Age", "0");
Basic usage of sharedpreferences