Android SharedPreferences使用
程序员文章站
2022-05-16 17:29:58
...
key_value存储介质,xml文档形式,存入路径data/data/{packagename}/shared_prefs下
private void dispose()
{
/*
* 获取 SharedPreferences 对象
*/
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
/*
* 获取 SharedPreferences.Editor 对象
*/
Editor editor = preferences.edit();
/*
* Editor 赋值
*/
editor.putString("name", SHARED_PREFERENCES_NAME);
editor.putString("hobby", "篮球、android");
/*
* Editor 游离态数据提交
*/
editor.commit();
read();
}
private void read()
{
/*
* 获取 SharedPreferences 对象
*/
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
String name = preferences.getString("name", "");
String hobby = preferences.getString("hobby", "");
Toast.makeText(DisposePreferences.this, "name value:" + name + "\n" + "hobby value:" + hobby, Toast.LENGTH_LONG).show();
}
private void dispose()
{
/*
* 获取 SharedPreferences 对象
*/
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
/*
* 获取 SharedPreferences.Editor 对象
*/
Editor editor = preferences.edit();
/*
* Editor 赋值
*/
editor.putString("name", SHARED_PREFERENCES_NAME);
editor.putString("hobby", "篮球、android");
/*
* Editor 游离态数据提交
*/
editor.commit();
read();
}
private void read()
{
/*
* 获取 SharedPreferences 对象
*/
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
String name = preferences.getString("name", "");
String hobby = preferences.getString("hobby", "");
Toast.makeText(DisposePreferences.this, "name value:" + name + "\n" + "hobby value:" + hobby, Toast.LENGTH_LONG).show();
}
上一篇: android 设备恢复,数据传递
下一篇: 老人保健需注意 4个误区慎入