For example, the following:
When we want to save our property settings, we need to sharedpreferences.
Above this small program, the music state is preserved. The state of the last exit used.
Enter the data file under the Ddms,data file, find your own package name file, double-click Open, there is a shared_prefs file, there is an XML file, this is the configuration file.
This program uses Android's up and down keys, the same as often our simulator's key is not available, this is the need for us to open it, in the C drive to find the file . Android, double-click the AvD file, locate the directory of your emulator, double-click to open, locate Config.ini, open, change Armhw.dpad=yes, start the simulator, then you can use it.
This procedure has established Mainactivity.java, Midiplayer.java, Activity_main.xml.
program code such as the following:
Mainactivity.java
Package Com.example.l3_sharedpreferences;import Com.example.l3_sharedpreferences.model.midiplayer;import Android.os.bundle;import Android.app.activity;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.view.keyevent;import Android.view.Menu;import Android.widget.textview;public class Mainactivity extends Activity {private TextView musicstateshow;private Midiplayer Midiplayer;private boolean musicsate=false; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); musicstateshow= (TextView) This.findviewbyid (r.id.musicstateshow); midiplayer=new Midiplayer (this); Sharedpreferences sharedpreferences=getsharedpreferences ("Music", mode_private); musicsate= Sharedpreferences.getboolean ("Mstate", false), if (musicsate) {musicstateshow.settext ("Current music Status: on"); Midiplayer.playmusic ();} Else{musicstateshow.settext ("Current music Status: Off");}} /** * Key Bounce Event */@Overridepublic boolean onKeyUp (int keycOde, KeyEvent Event) {System.out.println ("keycode=" +keycode); switch (keycode) {case KEYEVENT.KEYCODE_DPAD_UP: Musicstateshow.settext ("Current music Status: Open"); Musicsate=true;midiplayer.playmusic (); Break;case Keyevent.keycode_dpad_down: Musicstateshow.settext ("Current music Status: Off"); Musicsate=false;midiplayer.freemusic (); break;default:break;} Return Super.onkeyup (KeyCode, event);} /** * Press the event */@Overridepublic boolean onKeyDown (int keycode, keyevent event) {//To save data when exiting the application if (keycode== Keyevent.keycode_back) {sharedpreferences sharedpreferences=getsharedpreferences ("Music", MODE_PRIVATE); Editor Editor=sharedpreferences.edit (); Get Edit Object Editor.putboolean ("Mstate", musicsate); Join data editor.commit (); //Save if (musicsate) {midiplayer.freemusic ();} Finish ();} Return Super.onkeydown (KeyCode, event);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Midiplayer.java
Package Com.example.l3_sharedpreferences.model;import Java.io.ioexception;import Com.example.l3_sharedpreferences . R;import Android.content.context;import Android.media.mediaplayer;public class Midiplayer {public MediaPlayer Playermusic = null;private Context Mcontext = Null;public Midiplayer (context context) {Mcontext = context;} /* Play Music */public void Playmusic () {/* Load the music in the resource */playermusic = Mediaplayer.create (Mcontext, R.raw.start);/* Set whether to loop */playe Rmusic.setlooping (true); try {playermusic.prepare ();} catch (IllegalStateException e) {e.printstacktrace ();} catch ( IOException e) {e.printstacktrace ();} Playermusic.start ();} /* Stop and release music */public void Freemusic () {if (playermusic! = null) {playermusic.stop ();p layermusic.release ();}}}
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:id=" @+id/musicstateshow " android:layout_width=" Wrap_ Content " android:layout_height=" wrap_content " android:text=" @string/currentmusic "/></ Relativelayout>