Use sharedpreferences in Android
1. Use sharedpreferences 1
Protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); sharedpreferences mperferences = preferencemanager. getdefasharsharedpreferences (this); int counter = mperferences. getint ("counter", 1); textview mtextview = (textview) findviewbyid (R. id. showmssage); mtextview. settext ("counter is:" + counter); Editor meditor = mperferences. edit (); meditor. putint ("counter", ++ counter); meditor. commit ();}
Meditor. putint ("counter", ++ counter); writes the counter value to ++ counter, and finally submits.
Int counter = mperferences. getint ("counter", 1); obtains the value written to the couter. The default value is 1.
2. Create an SPF. xml file and write the data to the XML file. Finally, read the data in the XML file.
Protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); sharedpreferences = This. getsharedpreferences ("SPF", mode_private); Editor editor = sharedpreferences. edit (); Editor. putstring ("name", "James"); Editor. putint ("Age", 29); Editor. putboolean ("boy", true); Editor. commit (); log. I ("name", sharedpreferences. getstring ("name", "No Name"); log. I ("Age", integer. tostring (sharedpreferences. getint ("Age", 0); log. I ("boy", Boolean. tostring (sharedpreferences. getboolean ("boy", false )));}
Refer:
Sharedpreferences usage