Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14783
  • 博文数量: 11
  • 博客积分: 236
  • 博客等级: 二等列兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-10 20:30
文章分类

全部博文(11)

文章存档

2011年(11)

我的朋友

分类: 嵌入式

2011-07-22 15:50:37

Android保存数据的方式,有如下四种:
1、Shared Preferences:主要用于保存程序的系统配置信息。用来存储“key-values paires”。一般用于保存程序启动时设定的信息,以便在程序下一次启动时继续保留前一次设定的信息。
2、Files:用文件的形式保存信息。可以通过对文件的读写来获取或保存相关信息。
3、SQLite:用数据库的形式保存信息。SQLite是一个开源的数据库 系统。
4、NetWork:将数据保存于网络。

关键代码:
1、Shared Preferences
  1. /* ==================================
  2.               读取值
  3.  ================================== */
  4. // 取得活动的Preferences对象
  5. SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);

  6. // 取得值
  7. value = settings.getBoolean("key", false);

  8. /* ==================================
  9.               保存值
  10.  ================================== */
  11. // 取得活动的Preferences对象
  12. SharedPreferences uiState = getPreferences(Activity.MODE_PRIVATE);

  13. // 取得编辑对象
  14. SharedPreferences.Editor editor = uiState.edit();

  15. // 添加值
  16. editor.putBoolean("key", value);

  17. // 提交保存
  18. editor.commit();

生成的文件放在:/data/data/XXX/shared_prefs/...

2、Files
  1. /* ===================================
  2.              读取值
  3.  ================================== */
  4.  // 构建Properties对象
  5.  Properties properties = new Properties();

  6. // 打开输入文件
  7.  FileInputStream stream = this.openFileInput("inputfile");

  8. // 读取文件内容
  9. properties.load(stream);

  10. /* ===================================
  11.              保存值
  12.  ================================== */
  13. // 构建Properties对象
  14. Properties properties = new Properties();

  15. // 将数据打包成Properties
  16.  properties.put("key", "value");

  17.  // 打开输出文件
  18. FileOutputStream stream = this.openFileOutput("outputfile", mode);

  19. // 将打包好的数据写入文件
  20.  properties.store(stream, "");

阅读(978) | 评论(0) | 转发(0) |
0

上一篇:正则表达式

下一篇:Liskov替换原则

给主人留下些什么吧!~~