很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件或者xml进行保存。如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package
name>/shared_prefs目录下:
SharedPreferences sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE); Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("name", "名称"); editor.putInt("age", 4); editor.commit();//提交修改
生成的itcast.xml文件内容如下
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">名称</string> <int name="age" value="4" /> </map>
因为SharedPreferences背后是使用xml文件保存数据,getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后
缀,后缀会由Android自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,这四种模式前面介绍使用文件方式保存数据时已经讲解
过。如果希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指
定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。另外Activity还提供了另一个getPreferences(mode)方法操作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件
的名称。
访问SharedPreferences中的数据代码如下:
SharedPreferences sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE); //getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值 String name = sharedPreferences.getString("name", ""); int age = sharedPreferences.getInt("age", 1);
如果访问其他应用中的Preference,前提条件是:该preference创建时指定了Context.MODE_WORLD_READABLE或
者Context.MODE_WORLD_WRITEABLE权限。如:有个<packagename>为cn.itcast.action的应用使用下面语句创建了preference。
getSharedPreferences("itcast",Context.MODE_WORLD_READABLE);
其他应用要访问上面应用的preference,首先需要创建上面应用的Context,然后通过Context访问preference ,访问preference时会在应用所在包下的shared_prefs目录找到preference:
Context otherAppsContext = createPackageContext("cn.itcast.action", Context.CONTEXT_IGNORE_SECURITY); SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("itcast", Context.MODE_WORLD_READABLE); String name = sharedPreferences.getString("name", ""); int age = sharedPreferences.getInt("age", 0);
如果不通过创建Context访问其他应用的preference,也可以以读取xml文件方式直接访问其他应用preference对应的xml文件,如:
File xmlFile =new File(“/data/data/<package name>/shared_prefs/itcast.xml”);//<package
name>应替换成应用的包名
1、工程结构:
界面:
设置偏好xml生成的格式:
xml生成的文件目录:
2、string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, PreferencesActivity!</string> <string name="app_name">Preferences</string> <string name="name">姓名</string> <string name="age">年龄</string> <string name="savepre">保存参数</string> <string name="success">保存成功</string> </resources>
3、界面main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/age" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/age" android:numeric="integer" /><!-- 限制只能输入整型值 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/savepre" android:id="@+id/button" android:onClick="save" /> <!-- 用于指定显示界面所在的Activity中定义save方法 ,这里是PreferencesActivity--> </LinearLayout>
4、Activity
package cn.huangjie.preferences; import java.util.Map; import cn.huangjie.service.PreferencesService; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class PreferencesActivity extends Activity { private EditText nameText; private EditText ageText; private PreferencesService service; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText) this.findViewById(R.id.name); ageText = (EditText) this.findViewById(R.id.age); service = new PreferencesService(getApplicationContext()); //使显示界面的时候就把原有的值给赋值上 Map<String, String> params = service.getPreferences(); nameText.setText(params.get("name")); ageText.setText(params.get("age")); } public void save(View v){ // 默认的xml文件是该Activity的简单名称PreferencesActivity // this.getPreferences(MODE_PRIVATE); String name = nameText.getText().toString(); String age = ageText.getText().toString(); service.save(name, Integer.valueOf(age)); Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); } }
5、Service处理类
package cn.huangjie.service; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class PreferencesService { private Context context; public PreferencesService(Context context){ this.context = context; } /** * 保存设置偏好 * @param name * @param age */ public void save(String name, Integer age) { //设置文件是以xml来进行保存的 SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); Editor editor = preferences.edit(); editor.putString("name", name); editor.putInt("age", age); editor.commit(); } /** * 读取设置偏好 */ public Map<String, String> getPreferences(){ Map<String, String> params = new HashMap<String, String>(); SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); params.put("name", preferences.getString("name", "")); params.put("age", String.valueOf(preferences.getInt("age", 0))); return params; } }
工程下载路径:http://download.csdn.net/detail/wxwzy738/6272239