Android中,提供了4种数据存储方式:Shared PreferencesFilesSQLiteNetwork,但是这些方式存储的数据都是应用程序私有的,如果需要在应用程序之间共享数据,就需要使用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>

Android数据存储(一)_第1张图片

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

Android数据存储(一)_第2张图片

更多相关文章

  1. Android应用数据存储几种方式(1)
  2. Android 直接连MySQL数据库
  3. Color State List用于控件在各状态下的文本颜色显示
  4. android 多线程数据库读写分析与优化
  5. Android 数据持久化

随机推荐

  1. Android(安卓)Things APP版本更新解决方
  2. android 中文 API (41) —— RatingBar.O
  3. Android(安卓)四大组件之 Service(一)
  4. android地图定位
  5. Android(安卓)android:allowBackup waiti
  6. Android—— 4.2 Vold挂载管理_主体构建m
  7. [Android][SystemUI]navigationbar 3个虚
  8. Android预安装软件&adb命令&编译源码
  9. 81.s1-禁用checkBox点击事件
  10. android驱动学习-led次设备号(2)