Android学习笔记(八)-使用SharedPreferences进行数据存储
SharedPreferences sharedPreferences = getSharedPreferences("sharedData", Context.MODE_PRIVATE); Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("name", "无双"); editor.putInt("age", 24); editor.commit();//提交修改
生成的sharedData.xml文件内容如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">无双</string> <int name="age" value="24" /> </map>
因为SharedPreferences背后是使用xml文件保存数据,getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,
Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
如果希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。另外Activity还提供了另一个getPreferences(mode)方法操作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件的名称。
访问SharedPreferences中的数据代码如下:
SharedPreferences sharedPreferences = getSharedPreferences("sharedData", Context.MODE_PRIVATE);
//getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 1);
读、写其他应用的SharedPreferences
如果访问其他应用中的Preference,前提条件是:该preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限。
如:有个<package name>为com.geniusxiaoyu.sharedpreferences的应用使用下面语句创建了preference。
getSharedPreferences("sharedData", Context.MODE_WORLD_READABLE);
其他应用要访问上面应用的preference,首先需要创建上面应用的Context,然后通过Context 访问preference ,访问preference时会在应用所在包下的shared_prefs目录找到preference :
Context otherAppsContext = createPackageContext("com.geniusxiaoyu.sharedpreferences", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("sharedData", Context.MODE_WORLD_READABLE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 0);
上面程序中com.geniusxiaoyu.sharedpreferences就是其他程序的包名--实际上Android系统中就是用应用程序的包名来作为程序的标识的。
如果不通过创建Context访问其他应用的preference,也可以以读取xml文件方式直接访问其他应用preference对应的xml文件,如:
File xmlFile = new File(“/data/data/<package name>/shared_prefs/sharedData.xml”);//<package name>应替换成应用的包名
下面列出相关完整代码:
布局文件main.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:numeric="integer"
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="恢复参数"
android:id="@+id/resume"
/>
</LinearLayout>
</LinearLayout>
数据文件strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">软件参数保存</string>
<string name="name">网名</string>
<string name="age">年龄</string>
<string name="button">保存参数</string>
<string name="success">保存完成</string>
</resources>
MainActivity.java
package com.geniusxiaoyu.sharedpreferences;
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;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取输入值
nameText = (EditText)findViewById(R.id.name);
ageText = (EditText)findViewById(R.id.age);
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameText.getText().toString();
String age = ageText.getText().toString();
SharedPreferences preferences = getSharedPreferences("sharedData", Context.MODE_WORLD_READABLE);
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();
}
});
Button resumebutton = (Button)this.findViewById(R.id.resume);
resumebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("sharedData", Context.MODE_PRIVATE);
String name = preferences.getString("name", "");
int age = preferences.getInt("age", 20);
nameText.setText(name);
ageText.setText(String.valueOf(age));
}
});
}
}