Android 小项目之--数据存储【Shared Preferences】(附源码)
- 1、Shared Preferences。
用来存储 “键-值”格式的数据。 - 2、Files。
通过FileInputStream和FileOutputStream对文件进行操作。 - 3、SQLite。
标准数据库,支持SQL语句。 - NetWork。
通过网络存储和获取数据。
- 1、Shared Preferences 是什么?
- 2、如何使用 Shared Preferences 获得数据?
- 3、如何使用 Shared Preferences 保存数据?
- 3.1、使用getPreferences方法创建文件的模式。
- 4、模拟用户参数设置。
- 5、查看 Preferences 产生的文件。
上文获得Editor对象之后即可通过 Android 提供的各自操作函数进行数据提交,如:putBoolean()提交一个键值 为bool值的数据,最后通过commit方法保存这些数据。
- 文件创建模式:Activity.MODE_APPEND
如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。
- 文件创建模式:MODE_PRIVATE
默认模式,在那里创建的文件只能由应用程序调用,即为私有的
- 文件创建模式:Activity.MODE_WORLD_READABLE
允许所有其他应用程序有读取和创建文件的权限。
- 文件创建模式:Activity.MODE_WORLD_WRITEABLE
允许所有其他应用程序具有写入、访问和创建的文件权限。
音乐播放示例代码
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 sharedPreActivity extends Activity {
private TextView myTextView;
private CheckBox myBox;
private playMusic PLAYER = null ;
private boolean isplay = false ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.TextView01);
myBox = (CheckBox)findViewById(R.id.CheckBox01);
PLAYER = new playMusic( this );
/*
* 文件创建模式:Activity.MODE_APPEND
* 如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。
*/
/*
* 文件创建模式:MODE_PRIVATE
* 默认模式,在那里创建的文件只能由应用程序调用,即为私有的
*/
/*
* 文件创建模式:Activity.MODE_WORLD_READABLE
* 允许所有其他应用程序有读取和创建文件的权限。
*/
/*
* 文件创建模式:Activity.MODE_WORLD_WRITEABLE
* 允许所有其他应用程序具有写入、访问和创建的文件权限。
*/
SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
isplay = settings.getBoolean( " isplay " , false ); // 通过key值找到value,如果不存在即返回false
myBox.setChecked(isplay);
if (isplay){
myTextView.setText( " 当前音乐的播放状态:开 " );
isplay = true ;
PLAYER.Play();
}
else {
myTextView.setText( " 当前音乐的播放状态:关 " );
}
myBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked)
{
myTextView.setText( " 当前音乐的播放状态:开 " );
isplay = true ;
PLAYER.Play();
}
else {
myTextView.setText( " 当前音乐的播放状态:关 " );
isplay = false ;
PLAYER.FreeMusc();
}
}
});
}
@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK){
SharedPreferences uiState = getPreferences( 0 );
SharedPreferences.Editor editor = uiState.edit();
editor.putBoolean( " isplay " , isplay);
editor.commit();
if (isplay)
{
PLAYER.FreeMusc();
}
this .finish();
return true ;
}
return super .onKeyDown(keyCode, event);
}
}
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;
public class playMusic {
public MediaPlayer music = null ;
private Context myContext;
public playMusic(Context cc)
{
myContext = cc;
}
public void Play()
{
music = MediaPlayer.create(myContext, R.raw.start);
music.setLooping( true );
try {
music.prepare();
} catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
}
catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
music.start();
}
public void FreeMusc()
{
if (music != null )
{
music.stop();
music.release();
}
}
}
- 必须启动模拟器,运行程序。
- 在 你使用的IDE中切换到DDMS视图,选择File Explorer标签。注意:如果你的File Explorer里面没有任何东西,请根据此步骤找回--》DDMS-在Devices标签中选择
点击Reset adb即可将你的 File Explorer还原。 - 找到/data/data目录中对应的项目文件夹下的shared_prefs文件夹
