SharedPreference作用:
提供了一种轻量级的数据存取方法,主要存储数据比较少的配置信息。它以“key-value”对(类似于Map)的方式将数据保存在一个XML配置文件中。
存取数据方法:
使用来自于andorid.content包接口
取数据使用:SharedPreferences接口
存数据使用:SharedPreferences.Editor接口
SharedPreferences接口:
Context.getSharedPreferences(String name,int mode)方法得到SharedPreferences接口。
该方法的第一个参数是文件名称,第二个参数是操作模式。操作模式有三种:
MODE_PRIVATE(私有)
MODE_WORLD_READABLE(可读)
MODE_WORLD_WRITEABLE(可写)
int MODE= MODE_WORLD_READABLE+ MODE_WORLD_WRITEABLE
SharedPreferences接口的常用方法 :
SharedPreferences.Editor接口 :
SharedPreferences接口本身并没有提供写入数据的方法,而是通过SharedPreferences内部接口.
SharedPreferences调用edit()方法获取它所对应的Editor对象,该对象中提供了保存数据的方法
SharedPreferences.Editor editor = getSharedPreferences(TEMP_INFO, MODE_WORLD_WRITEABLE).edit();
SharedPreferences.Editor接口常用的方法 :
SharedPreferences的存储位置和格式:
DDMS的File Explore面板展开文件浏览树
SharedPreferences数据总是保存在/data/data/<package-name>/shared_prefs目录
SharedPreferences总是已XML格式保存
文件以<map>为根元素,每个子元素代表一个key-value对
读、写其他应用的SharedPreferences:
读取其他程序的SharedPreferences一般步骤:
通过CreatPackageContext()创建其他程序对应的Context
通过这个Context的getSharedPreferences()方法获取对应的SharedPreferences对象
如果需要写入数据,调用SharedPreferences的edit()方法获取对应的Editor对象
3个访问必需条件
共享者需要将SharedPreferences的访问模式设置为全局读或写
访问者需要知道共享者的包名称和SharedPreferences名称
访问者需要确切知道每个数据的名称和数据类型,用以正确读取数据
读写本应用SharedPreference实例代码:
public class SharedPreTest01Activity extends Activity {
SharedPreferences perfer;
SharedPreferences.Editor editor;
EditText edtzh, edtmm, edtdh;
Button btnzc, btndl, btndq;
Toast toast;
int mode = MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
perfer = getSharedPreferences("lgs000", mode);
editor = perfer.edit();
edtzh = (EditText) findViewById(R.id.edtzh);
edtmm = (EditText) findViewById(R.id.edtmm);
edtdh = (EditText) findViewById(R.id.edtdh);
btnzc = (Button) findViewById(R.id.btnzc);
btndl = (Button) findViewById(R.id.btndl);
btndq = (Button) findViewById(R.id.btndq);
btnzc.setOnClickListener(myListener);
btndl.setOnClickListener(myListener);
btndq.setOnClickListener(myListener);
}
OnClickListener myListener = new OnClickListener() {
@Override
public void onClick(View v) {
String zh = edtzh.getText().toString();
String mm = edtmm.getText().toString();
String dh = edtdh.getText().toString();
switch (v.getId()) {
case R.id.btnzc:
editor.putString("account", zh);
editor.putString("password", mm);
editor.putString("phone", dh);
editor.commit();
toast = Toast.makeText(SharedPreTest01Activity.this, "你的帐号:"
+ zh + " 密码:" + mm + " 电话:" + dh, 5000);
toast.show();
edtzh.setText("");
edtmm.setText("");
edtdh.setText("");
return;
case R.id.btndl:
if ((!zh.equals("") && zh != null)
&& (!mm.equals("") && zh != null))
if (zh.equals(perfer.getString("account", null))
&& mm.equals(perfer.getString("password", null)))
toast = Toast.makeText(SharedPreTest01Activity.this,
"登录成功!欢迎 " + zh + " 回来!!", 5000);
else
toast = Toast.makeText(SharedPreTest01Activity.this,
"帐号或密码错误,请重新输入!!", 5000);
else
toast = Toast.makeText(SharedPreTest01Activity.this,
"帐号或密码为空,请检查输入!!", 5000);
toast.show();
return;
case R.id.btndq:
if (perfer.contains("account") && perfer.contains("password")
&& perfer.contains("phone")) {
edtzh.setText(perfer.getString("account", null));
edtmm.setText(perfer.getString("password", null));
edtdh.setText(perfer.getString("phone", null));
} else {
toast = Toast.makeText(SharedPreTest01Activity.this,
"配置信息不存在或不完整!", 5000);
toast.show();
}
return;
}
}
};
}
读写非本应用SharedPreference实例代码:
public class SharedPreTest02Activity extends Activity {
Context myContext = null;
SharedPreferences perfer;
SharedPreferences.Editor editor;
EditText edtzh, edtmm, edtdh;
Button btngg, btndq;
Toast toast;
// int mode=MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
myContext = createPackageContext("cn.edu.zwu.tel",
Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
perfer = myContext.getSharedPreferences("lgs000",
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
editor = perfer.edit();
edtzh = (EditText) findViewById(R.id.edtzh);
edtmm = (EditText) findViewById(R.id.edtmm);
edtdh = (EditText) findViewById(R.id.edtdh);
btngg = (Button) findViewById(R.id.btngg);
btndq = (Button) findViewById(R.id.btndq);
btngg.setOnClickListener(myListener);
btndq.setOnClickListener(myListener);
}
OnClickListener myListener = new OnClickListener() {
@Override
public void onClick(View v) {
String zh = edtzh.getText().toString();
String mm = edtmm.getText().toString();
String dh = edtdh.getText().toString();
switch (v.getId()) {
case R.id.btndq:
if (perfer.contains("account") && perfer.contains("password")
&& perfer.contains("phone")) {
edtzh.setText(perfer.getString("account", null));
edtmm.setText(perfer.getString("password", null));
edtdh.setText(perfer.getString("phone", null));
} else {
toast = Toast.makeText(SharedPreTest02Activity.this,
"配置信息不存在或不完整!", 5000);
toast.show();
}
return;
case R.id.btngg:
editor.putString("account", zh);
editor.putString("password", mm);
editor.putString("phone", dh);
editor.commit();
toast = Toast.makeText(SharedPreTest02Activity.this, "你的帐号:"
+ zh + " 密码:" + mm + " 电话:" + dh, 5000);
toast.show();
edtzh.setText("");
edtmm.setText("");
edtdh.setText("");
return;
}
}
};
}