Learn Android Sharedpreferences use

Source: Internet
Author: User

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>









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.