在设计一个程序的时候通常会保存程序里面的一个参数,比如说我们andorid的手机的某个界面上填写了某些个人信息,希望这些信息保存在自己的手机当中,下次点击进去的时候就能够自动联想出来并填到相应的框框里面,这个时候我们前面的输入的一些参数就应该是得到保存的。提到参数保存,不得不提:SharedPreferences。
SharedPreferences 就是利用XML语言将相应的数据保存在相应的xml文件中,该xml文件可以供自己使用,也可以供其它应用程序来使用(需要合适的权限)。
创建一个SharedPreferences并保存数据到其中很简单,主要包含以下几个步骤:
1. 取得SharedPreferences:利用函数getSharedPreferences(),该函数有两个参数,参数1是名字,参数二时权限相关的,主要是告知该xml文件是私有的呢,还是可以供其它程序读或者写的。
2.获取一个getSharedPreferences的Editor对象,然后对该getSharedPreferences进行写操作。
3.写操作结束之后调用Editor对象的commit操作写到xml文件中。
在同一个程序中进行读getSharedPreferences文件基本类似。
代码如下:
PerferenceActivity.java
package com.andy.android.perference;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class PerferenceActivity extends Activity {
private EditText mEditName = null;
private EditText mEditAge = null;
private Button mButtonInsert = null;
private Button mButtonGet = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getViews();
/**
* 设置SharedPreferences,保存的路径为/data/data/com.andy.android.perference/shared_prefs下面
*/
mButtonInsert.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//取得编辑框里面的内容
String name = mEditName.getText().toString();
String age = mEditAge.getText().toString();
//取得SharedPreferences,模式为:MODE_WORLD_READABLE表示对其他应用程序是可读的。
SharedPreferences perference = getSharedPreferences("peronInfo", MODE_WORLD_READABLE);
//利用editor将编辑框的内容写到xml中去
SharedPreferences.Editor editor = perference.edit();
editor.putString("name", name);
editor.putInt("age", new Integer(age));
//以上的put内容是保存在内容中,完成后利用commit提交
editor.commit();
Toast.makeText(getApplicationContext(), "saved ok!", Toast.LENGTH_LONG).show();
}
});
/**
* 事件响应函数,用于取得保存在xml文件中的数据。
*/
mButtonGet.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//获取SharedPreferences
SharedPreferences perference = getSharedPreferences("peronInfo", getApplicationContext().MODE_PRIVATE);
//取得key为name的值,如果不存在,则返回空值
String name = perference.getString("name", "");
//取得key为age的值,如果不存在,则返回0
int age = perference.getInt("age", 0);
//设置编辑框里面的数据
mEditName.setText(name);
mEditAge.setText(String.valueOf(age));
}
});
}
/**
* 获取 ID
*/
public void getViews(){
mEditName = (EditText)findViewById(R.id.edit_name);
mEditAge = (EditText)findViewById(R.id.edit_age);
mButtonInsert = (Button)findViewById(R.id.inputto);
mButtonGet = (Button)findViewById(R.id.getfrom);
}
}
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/hello"
/>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<EditText
android:id="@+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入姓名"
/>
<TextView
android:id="@+id/age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/age"
/>
<EditText
android:id="@+id/edit_age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:hint="输入年龄,只能是整数哦"
/>
>
<Button
android:id="@+id/inputto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="插入数据到perference中"
/>
<Button
android:id="@+id/getfrom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从perference中提取数据"
/>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PerferenceActivity!</string>
<string name="app_name">Perference</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
</resources>
运行结果如下:
输入的是andyliu,年龄28,
然后我们到 /data/data/com.andy.android.perference/shared_prefs 下面可以看到一个xml文件,内容为:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">andyliu</string>
<int name="age" value="28" />
</map>
这个就是传说中的 SharedPreferences了。
我们清空掉其中的editText中的数据,然后点击从perference中提取数据就会发现,数据会显示到两个edit中了。
那么,如果再其它的程序中想要读取SharedPreferences中的数据肿么办呢?其实很简单,下面就一起看看怎么做。
这里为了图方便,在上一节的notify里面的button里面直接加代码来测试。
具体的内容在代码里面都说了,这里偶就不再罗嗦了。这里需要学习的重点是如何在其它的上下文中获得另一个包得上下文。
mButton2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//mNotifyManager.cancel(0);
try {
//利用getApplicationContext().createPackageContext("com.andy.android.perference", CONTEXT_IGNORE_SECURITY);来获取
//com.andy.android.perference包得上下文,参数1是包得名字,参数二代表忽视安全性问题。然后就和正常的preference读取操作一样了。
Context context = getApplicationContext().createPackageContext("com.andy.android.perference", CONTEXT_IGNORE_SECURITY);
SharedPreferences perference = context.getSharedPreferences("peronInfo", MODE_PRIVATE);
String name = perference.getString("name", "");
//取得key为age的值,如果不存在,则返回0
int age = perference.getInt("age", 0);
Toast.makeText(getApplicationContext(), "name:"+name+"age:"+age,Toast.LENGTH_LONG).show();
Log.i("ssssss", name+"~~"+age);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
直接贴上点击后的结果看看:
怎么样,得到了吧,大功告成~~~