本讲内容:SharedPreferences 和 Android中的文件IO操作
1、SharedPreferences
2、Android中的文件IO操作
Android中进行数据共享和数据存储有多种方式,前面我们讲过使用Sqlite数据库的方式,今天我们讲一下SharedPreferences和文件读写操作方式。
一、SharedPreferences
SharedPreferences是一种轻量级的数据存储方式,学过Web开发的同学,可以想象它是一个小小的Cookie。它可以用键值对的方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/包名/shared_prefs/)自己定义的xml文件中。下面我们用一个记录音乐播放进度的例子来学习SharedPreferences的使用。
1、建立一个新的项目 Lesson19_HelloSharedPreferences , Activity名字叫 MainHelloSharedPreferences.java
2、建立一个MusicService.java的Service,代码如下:
- package android.basic.lesson19;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.widget.Toast;
- public class MusicService extends Service {
- //定义MediaPlayer播放器变量
- MediaPlayer mPlayer = new MediaPlayer();
- @Override
- public void onCreate() {
- Toast.makeText(getApplicationContext(), "Service onCreate()", Toast.LENGTH_LONG).show();
- //创建播放器
- mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.babayetu);
- //设置自动循环
- mPlayer.setLooping(true);
- }
- @Override
- public IBinder onBind(Intent intent) {
- Toast.makeText(getApplicationContext(), "Service onBind()", Toast.LENGTH_LONG).show();
- //获得SharedPreferences对象
- SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE);
- //播放器跳转到上一次播放的进度
- mPlayer.seekTo(preferences.getInt("CurrentPosition", 0));
- //开始播放
- mPlayer.start();
- return null;
- }
- @Override
- public boolean onUnbind(Intent intent){
- Toast.makeText(getApplicationContext(), "Service onUnbind()", Toast.LENGTH_LONG).show();
- //获得SharedPreferences对象
- SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE);
- Toast.makeText(getApplicationContext(), "CurrentPosition="+mPlayer.getCurrentPosition(), Toast.LENGTH_LONG).show();
- //获得editor对象,写入一个整数到SharePreferences中,记住要用commit()提交,否则不会实现写入操作
- preferences.edit().putInt("CurrentPosition",mPlayer.getCurrentPosition()).commit();
- mPlayer.stop();
- return false;
- }
- }
复制代码3、更改AndroidManifest.xml内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest android:versionname="1.0" android:versioncode="1" xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson19">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:label="@string/app_name" android:name=".MainHelloSharedPreferences">
- <intent -filter="">
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category></action></intent>
- </activity>
- <service android:name=".MusicService" android:enabled="true">
- </service>
- </application>
- <uses android:minsdkversion="8" -sdk="">
- </uses></manifest>
复制代码4、res/layout/mail.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:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="SharedPreferences的使用" android:id="@+id/TextView01">
- </textview>
- <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="绑定音乐播放服务" android:id="@+id/Button01" android:layout_margintop="10dp">
- </button>
- <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="解绑定音乐播放服务" android:id="@+id/Button02" android:layout_margintop="10dp">
- </button>
- </linearlayout>
复制代码5、MainHelloSharedPreferences.java的内容如下:
- package android.basic.lesson19;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainHelloSharedPreferences extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //定义UI组件
- Button b1 = (Button) findViewById(R.id.Button01);
- Button b2 = (Button) findViewById(R.id.Button02);
- //定义ServiceConnection对象
- final ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- //定义按钮的单击监听器
- OnClickListener ocl = new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainHelloSharedPreferences.this,
- android.basic.lesson19.MusicService.class);
- switch (v.getId()) {
- case R.id.Button01:
- Toast.makeText(getApplicationContext(), "Button01 onClick", Toast.LENGTH_LONG).show();
- //绑定服务
- bindService(intent,conn,Context.BIND_AUTO_CREATE);
- break;
- case R.id.Button02:
- Toast.makeText(getApplicationContext(), "Button02 onClick", Toast.LENGTH_LONG).show();
- //取消绑定
- unbindService(conn);
- break;
- }
- }
- };
- //绑定单击监听器
- b1.setOnClickListener(ocl);
- b2.setOnClickListener(ocl);
- }
- }
复制代码6、运行程序,查看运行情况:
查看 File Explorer,在/data/data/android.basic.lesson19/shared_prefs/目录下有一个MusicCurrentPosition.xml文件,点击右上角的按钮 pull a file from the device,可以把这个xml文拷贝出来
7、查看MusicCurrentPosition.xml的内容,可以看到音乐播放进度的数据存贮在这个xml中
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <int name="CurrentPosition" value="15177">
- </int></map>
复制代码兴趣的同学可以尝试一下,在Activity中增加一个按钮,点击以后把SharedPreference中的播放进度数据取出来,显示在另一个文本框Textview02里,我在这里把最后的运行结果图放这里,代码你们可以自己练习着敲出来,从中体会Share的意思,是不是在同一个APK中不同的组件之间都可以去访问这个共享的持久化数据?从这一点上说是不是有点像是Cookie?
二、Android中的文件IO操作(待续)