Sharedpreference use Getsharedpreference (name,mode) or this sharedpreference, the following example, the first time obviously there is no such file. This method can be called in any context in the app, and this method belongs to a context, which requires the Sunshine Tool class, which has the following method:
getpreferredlocation (Context context) { Sharedpreferences prefs = Preferencemanager. getdefaultsharedpreferences (context) ; prefs.getstring (context.getstring (r.string. Pref_location_key), context.getstring (r.string. Pref_location_default));}
A little bit different, it should be a question of preference, a discussion. After obtaining sharedpreference, reading is very simple, call getstring,getint and other methods, if you want to write, Need to obtain a sharedpreference.edtior, can be obtained through the Sharedpreference.edit () method, and then call the editor Putint putstring, and other methods to write,Finally, be aware that you must call the Editor.commit method。 This data is presented in the following complete example.
Public classMainactivityextendsappcompatactivity {sharedpreferencesPreferences; Sharedpreferences.editorEditor;Public static FinalStringPre_tag="Sy.com";Public static FinalStringKey_time="Time";Public static FinalStringKey_random="Random"; @Override protected voidonCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.Activity_main); Preferences= Getsharedpreferences (Pre_tag, mode_private); Editor=Preferences. Edit (); Button read = (button) Findviewbyid (r.id.Read); Button write = (button) Findviewbyid (r.id.Write); Read.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick(View v) {String time =Preferences. getString (Key_time, NULL);intRandom =Preferences. GETINT (Key_random, 0); String result = Time = =NULL?"Data not currently written":"Last Write Time"+ Time +"\ nlast generated random number "+ Random; Toast.Maketext(Getapplicationcontext (), Result, Toast.Length_short). Show (); } }); Write.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick(View v) {SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); Editor. putstring (Key_time, Sdf.format (NewDate ())); Editor. Putint (Key_random,(int) (Math.Random() * -)); Editor. Commit (); } }); }}
Setting's preferencepreference translation is a preference, set the user's preferences. Instead of setting the user interface with the View object, set the various subclasses that are declared in the XML file using preference. A Preference object is a separate set of construction components. Each preference is displayed as a list item, providing the user with settings. Each preference we add has a corresponding key value pair saved in a default sharedpreference file, the user modifies the settings, and the contents of the Sharedpreference file are modified. We don't have to write, we just need to read it when we use it. Because it is not constructed with a view, the display of this setting page requires some special subclasses, before 3.0, after inheriting preferenceactivity,3.0, you can use the traditional activity to hold a Preferencefragment object. You can also use preferenceactivity to create a two-sided layout for a large screen.
The difference between each of the preferred options, must have a key, this key specifies the key in the Sharedpreference file, use him to get the value of this preference option, the Android system helps us maintain the update of this preference, all we have to do is read.
Data storage (1)--key-value pairs