Android学习指南之十八:共享参数类SharedPreferences的使用
2012-07-31 20:50
591 查看
Android系统在数据存储和数据共享方面提供了多种方式,包括前面我们讲过的使用xml文件中。下面我们用一个记录音乐播放进度的例子来学习SharedPreferences的使用。
二、SharedPreferences实例
1、建立一个新的项目Lesson19_HelloSharedPreferences,Activity名字叫MainHelloSharedPreferences.java。
2、建立一个MusicService.java的Service,代码如下:
查看File Explorer,在/data/data/android.basic.lesson19/shared_prefs/目录下有一个MusicCurrentPosition.xml文件,点击右上角的按钮pull a file from the device,可以把这个xml文拷贝出来:
7、查看MusicCurrentPosition.xml的内容,可以看到音乐播放进度的数据存贮在这个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内容如下:
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添加一个按钮,点击它实现的功能是取出SharedPreferences中的播放进度数据,显示在另一个文本框Textview02里。下面直接贴上最终的运行效果图,至于程序,大家可以试着自己写一下,更加深刻的理解Share,同一个进程中的不同组件是否都可以访问此共享的持久化数据?这一点是不是与Cookie有些类似?
相关文章推荐
- Android源码学习(1)Git使用指南
- Android 9Patch图片的使用-android学习之旅(十八)
- Android学习指南之三十四:Android定时器Timer的使用
- Android学习指南之十五:SQLite数据库及其使用实例
- Android学习之 使用ContentProvider实现数据共享
- Android 9Patch图片的使用-android学习之旅(十八)
- Android学习指南之二十九:WebView使用详解
- Android学习指南之十五:SQLite数据库及其使用实例
- Android学习19--使用ContentProvider实现数据共享
- Android 9Patch图片的使用-android学习之旅(十八)
- Android学习总结(十八) ———— SQLite数据库使用
- Android学习指南之三:Android模拟器的使用方法
- Android学习指南之二十七:如何使用Handler
- Android学习指南之二十一:Broadcast Receiver 的使用
- Android学习指南之三十一:Android中使用SAX和pull方式解析XML
- android学习:使用它ContentProvider获取其他程序共享的数据
- Android开发学习之使用ContentProvider实现数据共享
- Android学习——Android Realm数据库使用指南
- Android学习指南之三十:使用URLConnection和HttpClient访问网络的方法