您的位置:首页 > 移动开发 > Android开发

Android数据存储(一)

2011-03-13 13:22 281 查看
在Android中,提供了4种数据存储方式:Shared Preferences、Files、SQLite、Network,但是这些方式存储的数据都是应用程序私有的,如果需要在应用程序之间共享数据,就需要使用Android提供的ContentProvider机制。

一、SharedPreferences
主要针对系统配置信息的保存,采用KVP(key-value paires)格式存储数据,只可存储基本数据类型。SharedPreferences类似于我们常用的.ini文件,保存应用程序的属性设置。

SharedPreferencesDemo.java
package com.example.android.sharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
public class SharedPreferencesDemo extends Activity
{
private MIDIPlayer mMIDIPlayer = null;
private boolean mMusic = false;
private TextView mTextView = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTextView = (TextView)findViewById(R.id.tv01);
mMIDIPlayer = new MIDIPlayer(this);

//Retrieve a SharedPreferences object for accessing preferences that are private to this activity
SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
mMusic = settings.getBoolean("music", false);

if(mMusic)
{
mTextView.setText("当前音乐状态:开");
mMusic = true;
mMIDIPlayer.playMusic();
}
else
{
if(mTextView == null)
{
Log.d("mTextView == null", "mTextView == null");
}

mTextView.setText("当前音乐状态:关");
}
}

public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_UP:
{
mTextView.setText("当前音乐状态:开");
mMusic = true;
mMIDIPlayer.playMusic();
}
break;

case KeyEvent.KEYCODE_DPAD_DOWN:
{
mTextView.setText("当前音乐状态:关");
mMusic = false;
mMIDIPlayer.freeMusic();
}
break;
}
return true;
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//Retrieve a SharedPreferences object for accessing preferences that are private to this activity
SharedPreferences uiState = getPreferences(Activity.MODE_PRIVATE);
//Create a new Editor for these preferences, through which you can make modifications to the data
//in the preferences and atomically commit those changes back to the SharedPreferences object
SharedPreferences.Editor editor = uiState.edit();
editor.putBoolean("music", mMusic);
//Commit your preferences changes back from this Editor to the SharedPreferences object it is editing.
//This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.
editor.commit();

if(mMusic)
{
mMIDIPlayer.freeMusic();
}

this.finish();

return true;
}

return super.onKeyDown(keyCode, event);
}
}

MIDIPlayer.java
package com.example.android.sharedpreferences;
import android.content.Context;
import android.media.MediaPlayer;
public class MIDIPlayer
{
private MediaPlayer mMediaPlayer = null;
private Context mContext = null;

public MIDIPlayer(Context context)
{
mContext = context;
}

public void playMusic()
{
mMediaPlayer = MediaPlayer.create(mContext, R.raw.start);

mMediaPlayer.setLooping(true);

mMediaPlayer.start();
}

public void freeMusic()
{
if(mMediaPlayer != null)
{
mMediaPlayer.stop();
mMediaPlayer.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/tv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TextView>
</LinearLayout>



现在我们已经通过Preferences来存取数据了。在安装每个应用程序时,在/data/data目录下都会产生一个文件夹,如果应用程序中使用了Preferences,那么便会在/data/data下产生一个shared_prefs文件夹,在其中保存我们的数据:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: