學習Android之SharedPreferences使用

來源:互聯網
上載者:User

標籤:xml   android   sharedpreferences   

如下:



當我們想讓自己的屬性設定儲存下來,這時就需要SharedPreferences。

上面這個小程式,音樂狀態是儲存下來的。使用的上一次退出的狀態。

進入DDMS,data檔案下的data檔案中,找到自己的包名檔案,雙擊開啟,有shared_prefs檔案,裡面有xml檔案,這就是設定檔。

本程式使用了android的上下鍵,同常我們的模擬器的方向鍵是不能用的,這是就需要我們開啟它,在C盤下找到檔案 .android,雙擊avd檔案,找到你的模擬器的檔案夾,雙擊開啟,找到config.ini,開啟,修改armhw.dPad=yes,啟動模擬器,這時就能用了。


本程式建立了MainActivity.java、MIDIPlayer.java、activity_main.xml。


程式碼如下:

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("當前音樂狀態:開");midiPlayer.playMusic();}else{musicStateShow.setText("當前音樂狀態:關");}}/** * 按鍵彈起事件 */@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {System.out.println("keyCode="+keyCode);switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:musicStateShow.setText("當前音樂狀態:開");musicSate=true;midiPlayer.playMusic();break;case KeyEvent.KEYCODE_DPAD_DOWN:musicStateShow.setText("當前音樂狀態:關");musicSate=false;midiPlayer.freeMusic();break;default:break;}return super.onKeyUp(keyCode, event);}/** * 按鍵按下事件 */@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {//退出應用程式時儲存資料if(keyCode==KeyEvent.KEYCODE_BACK){SharedPreferences sharedPreferences=getSharedPreferences("music", MODE_PRIVATE);Editor editor=sharedPreferences.edit();  //取得編輯對象editor.putBoolean("mState", musicSate);   //添加資料editor.commit();     //儲存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;}/* 播放音樂 */public void playMusic() {/* 裝載資源中的音樂 */playerMusic = MediaPlayer.create(mContext, R.raw.start);/* 設定是否迴圈 */playerMusic.setLooping(true);try {playerMusic.prepare();} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}playerMusic.start();}/* 停止並釋放音樂 */public void freeMusic() {if (playerMusic != null) {playerMusic.stop();playerMusic.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>









聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.