Package Com.example.sharedemo01;import Android.app.activity;import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.Bundle; Import Android.util.log;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Save data to sharedpreferences,//the first parameter is a file name, customizable, to fetch the data with this name to fetch///The second parameter is stored in the way sharedpreferences share = this.getsharedpreferences ("Share", context.mode_private);//Get editor Editor editor = Share.edit ();//through the editor to save, is the map, with the key value of the memory editor.putstring ("username "," "admin"); editor.putstring ("Password", "123456");//Perform task Editor.commit ();} @Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume ();//Fetch data sharedpreferences share = th Is.getsharedpreferences ("Share", context.mode_private);//With a key, the second value is the default value string user = Share.getstring ("username", "") ; String pwd = share.getstring ("Password", "");}}
Share.xml
11th chapter, sharedpreferences Access (Android)