Android数据储存之SharePreference
Android数据存储之SharePreference
Shared Preferences:是用来存储“key-value”格式的数据,它是一个轻量级的键值存储机制
只可以用来存储基本的数据类型。
1.主要是针对系统配置信息的保存如给程序界面设置了音效,想在下一次启动程序时还能使用上次设置的音效。
Activity:
package com.ko8e; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.KeyEvent; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.CompoundButton.OnCheckedChangeListener; public class MyActivity extends Activity { /** Called when the activity is first created. */ private TextView view = null; private CheckBox checkbox = null; private MIDIPlayer PLAYER= null; private boolean isplay = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PLAYER = new MIDIPlayer(this); view = (TextView) findViewById(R.id.view); checkbox = (CheckBox) findViewById(R.id.checkbox); /* * 文件创建模式:Activity.MODE_APPEND * 如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。 */ /* * 文件创建模式:MODE_PRIVATE * 默认模式,在那里创建的文件只能由应用程序调用,即为私有的 */ /* * 文件创建模式:Activity.MODE_WORLD_READABLE * 允许所有其他应用程序有读取和创建文件的权限。 */ /* * 文件创建模式:Activity.MODE_WORLD_WRITEABLE * 允许所有其他应用程序具有写入、访问和创建的文件权限。 */ SharedPreferences share = getPreferences(Activity.MODE_PRIVATE); isplay = share.getBoolean("isplay", false); if(isplay) { view.setText("当前音乐状态: 开"); isplay = true; PLAYER.PlayMusic(); } else { view.setText("当前音乐状态:关"); } checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { view.setText("当前音乐状态: 开"); isplay = true; PLAYER.PlayMusic(); } else { view.setText("当前音乐状态:关"); isplay = false; PLAYER.FreeMusic(); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ SharedPreferences uiState=getPreferences(0); SharedPreferences.Editor editor=uiState.edit(); editor.putBoolean("isplay", isplay); editor.commit(); if(isplay) { PLAYER.FreeMusic(); } this.finish(); return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return super.onKeyUp(keyCode, event); } }
另一个封装类:
package com.ko8e; import java.io.IOException; 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(); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <CheckBox android:id="@+id/checkbox" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>