/********************************************************************************************
* author:conowen@大钟
* E-mail:conowen@hotmail.com
* http://blog.csdn.net/conowen
* 注:本文为原创,仅作为学习交流使用,转载请标明作者及出处。
********************************************************************************************/
1、SharedPreferences的简单介绍 应用程序在运行的时候,可能会随着用户的使用而保持该用户的配置信息,如上次播放时的eq设置,音量设置,上网的cookies信息等等,这些小量 的信息可以通过SharedPreferences来保持,通过SharedPreferences保持的数据为一个XML文件,位于应用程序的私有文件夹。 2、具体操作方法 获取SharedPreferences,可以通过以下方法获取 public SharedPreferences getSharedPreferences (String name, int mode)Since: API Level 1
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
Parametersname | Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). | mode | Operating mode. Use 0 or MODE_PRIVATE for the default operation,MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE to control permissions. The bitMODE_MULTI_PROCESS can also be used if multiple processes are mutating the same SharedPreferences file.MODE_MULTI_PROCESS is always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions. |
Returns- Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.
参数简述: Name————获得SharedPreferences之后,将会在应用程序的私有文件夹中保存着一个XML文件,第一个参数name就是这个文件名字。 Mode————XML文件的保存模式,默认为0,也就是MODE_PRIVATE
3、简单的demo 通过service的一个音乐播放例子,“播放”与“暂停”两个按钮 暂停之后,保持播放进度到SharedPreferences里面,然后再次播放的话,读取进度值进行音乐播放。 - /*
- * @author:conowen
- * @date:12.3.01
- *
- */
- package com.conowen.sharedpreferences;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class SharedPreferencesActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button play = (Button) findViewById(R.id.play);
- Button pause = (Button) findViewById(R.id.pause);
-
- final Intent intent = new Intent(SharedPreferencesActivity.this,service.class);
- // 定义intent为final,全局变量,供下面两个匿名内部类(onclicklisenter)使用
- play.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- startService(intent);
- // 开始播放,通过前面定义好的intent传递
-
- }
- });
- pause.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- stopService(intent);
- // 停止播放
-
- }
- });
-
- }
- }
复制代码 第二个class是继承service的,记得在manifest.XML里面注册service- /*
- * @author:conowen
- * @date:12.3.01
- *
- */
- package com.conowen.sharedpreferences;
- import android.app.Service;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- public class service extends Service {
- MediaPlayer player;
- // 新建播放器
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- player = MediaPlayer.create(this, R.raw.lt26);
- player.setLooping(true);
- // 两种方式的第一个生命周期都是onCreate()
- }
- @Override
- public void onStart(Intent intent, int startId) {
- // TODO Auto-generated method stub
- super.onStart(intent, startId);
- SharedPreferences sp = this.getSharedPreferences("music_progress",
- MODE_PRIVATE);
- // music_progress为XML文件的文件名
- player.seekTo(sp.getInt("progress", 0));
- // progress为键值对中的key,progress所对应的值通过player.getCurrentPosition()获得,请看下面详细
- // 每次调用onStart方法时,就会设置player的进度
- // MediaPlayer的seekTo方法,读取进度,然后播放,getInt的第一个参数是所要读取的key名字,第二个参数为默认初始值。也就是说进度初始默认为0
- player.start();
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- SharedPreferences sp = this.getSharedPreferences("music_progress",
- MODE_PRIVATE);
- sp.edit().putInt("progress", player.getCurrentPosition()).commit();
- // player.getCurrentPosition()方法获取播放进度的数据
- // commit()方法是保存SharedPreferences获取来的数据
- player.stop();
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- }
复制代码通过SharedPreferences保存的文档如下所示,打开DDMS,切换到File Explore,在私有目录下的shared_prefs文件夹里面,路径为 /data/data/你的包名/shared_prefs
内容如下 - <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <int name="progress" value="90695" />
- </map>
复制代码 |