android 使用sharedPreferences保存用户设置的参数
2012-06-20 00:09
399 查看
官方文档介绍:
The
and retrieve persistent key-value pairs of primitive data types. You can use
save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
Shared preferences are not strictly for saving "user preferences," such as what ringtone a user has chosen. If you're interested in creating user preferences for your application, see
which provides an Activity framework for you to create user preferences, which will be automatically persisted (using shared preferences).
To get a
multiple preferences files identified by name, which you specify with the first parameter.
Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
To write values:
Call
Add values with methods such as
Commit the new values with
To read values, use
a.示例程序:保存用户设置的数据
在保存之后,sharedPreferences将会把数据保存在 /data/data/<应用程序包名>/shared_pres/目录下,且使用xml方式保存数据
示例程序保存的数据是:
b.示例程序:从sharedPreferences中读取数据
Using Shared Preferences
The SharedPreferencesclass provides a general framework that allows you to save
and retrieve persistent key-value pairs of primitive data types. You can use
SharedPreferencesto
save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
User Preferences
Shared preferences are not strictly for saving "user preferences," such as what ringtone a user has chosen. If you're interested in creating user preferences for your application, seePreferenceActivity,
which provides an Activity framework for you to create user preferences, which will be automatically persisted (using shared preferences).
To get a
SharedPreferencesobject for your application, use one of two methods:
getSharedPreferences()- Use this if you need
multiple preferences files identified by name, which you specify with the first parameter.
getPreferences()- Use this if you need only one preferences file for your
Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
To write values:
Call
edit()to get a
SharedPreferences.Editor.
Add values with methods such as
putBoolean()and
putString().
Commit the new values with
commit()
To read values, use
SharedPreferencesmethods such as
getBoolean()and
getString().
a.示例程序:保存用户设置的数据
/** * 保存各项参数 * @param name 姓名 * @param age 年龄 */ public void save(String name, int age) { SharedPreferences preferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE); Editor edit = preferences.edit(); edit.putString("name", name); edit.putInt("age", age); edit.commit(); }
在保存之后,sharedPreferences将会把数据保存在 /data/data/<应用程序包名>/shared_pres/目录下,且使用xml方式保存数据
示例程序保存的数据是:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <int name="age" value="23" /> <string name="name">g形成</string> </map>
b.示例程序:从sharedPreferences中读取数据
/** * @return 返回参数设置数据 */ public Map<String, String > getPreferences(){ Map<String, String> params = new HashMap<String, String>(); SharedPreferences sp = context.getSharedPreferences("preferences", Context.MODE_PRIVATE); params.put("name", sp.getString("name", "")); params.put("age", String.valueOf(sp.getInt("age", 0))); return params; }
相关文章推荐
- Android采用SharedPreferences保存用户偏好设置参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数
- android第十三步采用SharedPreference保存用户偏好设置参数
- Android心得4.2--SharedPreference保存用户偏好参数设置.doc
- 保存、方法-Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数-by小雨
- Android之使用SharedPreferences保存用户偏好参数
- Android开发入门之采用SharedPreferences保存用户偏好设置参数
- android利用SharedPreferences来保存用户参数及读取
- Android之使用RatingBar设置游戏难度并保存
- saveInstallState参数使用详解(android activity状态保存和恢复)
- 实际用户ID,有效用户ID,保存的设置用户ID 使用
- Android 史上最强多语言国际化,不仅第一次会跟随系统,而且会保存用户的语言设置
- Android应用中为用户保存偏好设置(传智播客视频笔记)
- PreferencesActivity与PreferencesFragment结合使用进行参数设置选项保存
- Android 使用 SharedPreferences 保存和加载软件参数
- Android应用中为用户保存偏好设置(传智播客视频笔记)
- Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数
- Android-如何将用户设置进行保存?(内部存储、SD卡,用类作为媒介)
- Android基础入门教程——6.2 数据存储与访问之——SharedPreferences保存用户偏好参数