Android 存储之SharedPreferences
来源:互联网 发布:网络歌手好听的歌2015 编辑:程序博客网 时间:2024/06/09 17:25
SharedPreferences存储常用在保存软件参数的配置。
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件或者xml进行保存。如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:
SharedPreferencessharedPreferences = 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中的数据代码如下:
SharedPreferencessharedPreferences = 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);
SharedPreferencessharedPreferences = 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>应替换成应用的包名
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="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"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2"
android:id="@+id/button2"
/>
</LinearLayout>
</LinearLayout>
src文件:
package cn.itcast.set;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText;
private EditText ageText;
@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);
Button button = (Button)this.findViewById(R.id.button);
Button button2 = (Button)this.findViewById(R.id.button2);
ButtonClickListener listener = new ButtonClickListener();
button2.setOnClickListener(listener);
button.setOnClickListener(listener);
}
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("itcast", Context.MODE_WORLD_READABLE);
switch (v.getId()) {
case R.id.button:
String name = nameText.getText().toString();
String age = ageText.getText().toString();
Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", new Integer(age));
editor.commit();
Toast.makeText(MainActivity.this, R.string.success, 1).show();
break;
case R.id.button2:
nameText.setText(preferences.getString("name", ""));
ageText.setText(String.valueOf(preferences.getInt("age", 20)));
break;
}
}
}
}
- Android 存储之SharedPreferences
- Android 存储之SharedPreferences
- Android存储之SharedPreferences
- Android 之SharedPreferences存储
- Android存储之SharedPreferences
- Android存储之SharedPreferences
- Android存储之SharedPreferences
- Android之SharedPreferences存储
- Android存储之SharedPreferences存储
- Android数据存储之SharedPreferences
- Android数据存储之SharedPreferences
- Android--数据存储之SharedPreferences
- android数据存储之SharedPreferences
- Android之数据存储SharedPreferences
- Android数据存储之SharedPreferences
- android数据存储之--------- SharedPreferences
- Android数据存储之SharedPreferences
- Android数据存储之SharedPreferences
- SVN版本冲突解决详解
- ibatis 笔记
- Android tcpdump
- 飞思卡尔i.MX6系列应用处理器进入量产阶段
- Warning: mysql_connect(): [2002]的错误解决方案
- Android 存储之SharedPreferences
- 实现完整单元测试的思路和方法
- fibnacci(1)
- 编译模块--Makefile
- 编译MSM7627a碰到的问题
- 理解SQL SERVER中的逻辑读,预读和物理读
- 软件工程
- BSD socket
- 越狱iOS设备利用itms-services协议,实现Safari一键安装IPA