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文件夹,在其中保存我们的数据:
一、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文件夹,在其中保存我们的数据:
相关文章推荐
- Android数据存储五种方式总结
- Android杂谈之Android数据存储方式
- android(17)_数据存储与访问_SQLite数据库_修改数据库
- Android-数据储存:用户登录界面(存储)
- Android的数据存储和IO - 自动朗读(TTS)
- Android数据存储方式—SQLite
- Android数据存储和界面展现
- android数据存储之外部存储(External Storage)
- 处女男学Android(十四)---Android 重量级数据存储之SQLite
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
- android 5种存储数据方式
- Android实现数据存储技术
- 为什么不能往Android的Application对象里存储数据
- android登录实现,存储数据到/data/data/包名/info.txt
- Android数据存储(三)----- SQLite数据库存储
- Android中数据存储
- Android应用之——不要将数据存储在Application类中
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)
- Android核心基础-5.Android 数据存储与访问-4.ContentProvider 内容提供者