Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 學習Android之SharedPreferences使用

學習Android之SharedPreferences使用

編輯:關於Android編程

效果圖如下:

vc7EvP4gPHN0cm9uZz4uYW5kcm9pZDwvc3Ryb25nPqOsy6u792F2ZM7EvP6jrNXStb3E47XExKPE4sb3tcTOxLz+vNCjrMuru/e08r+qo6zV0rW9PHN0cm9uZz5jb25maWcuaW5pPC9zdHJvbmc+o6y08r+qo6zQ3rjEYXJtaHcuZFBhZD15ZXOjrMb0tq/Eo8TixvejrNXiyrG+zcTc08PBy6GjPC9wPgo8cD48YnI+CjwvcD4KPHA+sb6zzNDyvajBosHLTWFpbkFjdGl2aXR5LmphdmGhok1JRElQbGF5ZXIuamF2YaGiYWN0aXZpdHlfbWFpbi54bWyhozwvcD4KPHA+PGJyPgo8L3A+CjxwPrPM0PK0+sLryOfPwqO6PC9wPgo8cD5NYWluQWN0aXZpdHkuamF2YTxicj4KPC9wPgo8cD48YnI+CjwvcD4KPHA+PHByZSBjbGFzcz0="brush: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; @Override protected 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("當前音樂狀態:關"); } } /** * 按鍵彈起事件 */ @Override public 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); } /** * 按鍵按下事件 */ @Override public 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); } @Override public 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



    










  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved