Using Sharedpreferences to save data, the essence is to use the XML file to store data,
Storage location:/data/data/<package name>/shared_prefs
Write:
Public voidwritedata (view view) {//instantiate the Sharedpreferences object, parameter 1 is the name of the stored file, parameter 2 is the way the file is opened, and when the file does not exist, it is created directly, and if it exists, it is used directlySharedpreferences mysharepreferences =getsharedpreferences ("test", activity.mode_private); //instantiating a Sharedpreferences.editor objectSharedpreferences.editor Editor =Mysharepreferences.edit (); //Save the data in a putstring wayEditor.putstring ("name", "tinyphp"); //Submit DataEditor.commit (); //Prompt SuccessToast.maketext ( This, "Data successfully written", Toast.length_short). Show (); }
Mode is default to 0 or mode_private
Mode_private, read only, accessible only by its own application
Mode_world_readable, readable, in addition to their own access can also be read by other programs should be
Mode_world_writeable, can be written, in addition to their own access can also be read by other programs and write
Read:
Public void readdata (view view) { // Instantiate Sharedpreferences object sharedpreferences Myshareperferences =getsharedpreferences ("Test", activity.mode_private); // Get values with GetString String name =myshareperferences.getstring ("name", ""); // Show Toast.maketext (This, "read data \ n" + "name:\n" +name, Toast.length_short). Show () ;
Android: Storage mode sharepreferences