当前位置: 首页 > news >正文

【Android】数据存储之SharedPreferences

1.适用场合:类似于ini文件,用来保存程序的一些属性设置

2.使用方式:通过getSharedPreferences方法获得Preferences对象,获得editor对象,使用editor.put……()方法添加数据,最后通过commit()方法保存这些数据

ShardPreferences mState = this,getPrefersences();

ShardPreferences.Editor editor = mState.edit();

editor.putint("num",99);

editor.apply(); //注意这边官方推荐使用apply来替代commit()提交数据,这样可以避免引起ANR,commit()操作会需要在UI线程进行等待

SharedPreferences preferences = getSharedPreferences("kesenhoo", Context.MODE_PRIVATE); String name = preferences.getString("name", ""); int age = preferences.getInt("age", 20); nameText.setText(name); ageText.setText(String.valueOf(age));

3.读取参数:

SharedPreferences preferences = getSharedPreferences("kesenhoo", Context.MODE_PRIVATE); String name = preferences.getString("name", ""); int age = preferences.getInt("age", 20); nameText.setText(name); ageText.setText(String.valueOf(age));

4.保存参数:自动保存到XML文件,路径为/data/data/包名/shared_prefs文件夹下


5.注意事项:默认情况其他应用程序不可以访问本程序的SharedPreferences文件,如果需要访问其他应用程序的SharedPreferences的配置文件,可以使用如下方法:

关键点:学习如何构造其他应用的上下文对象

Context otherContext = getContext().createPackageContext("com.hoo.preferences",Context.CONTEXT_IGNORE_SECURITY);

SharedPreferences preferences = otherContext.getSharedPreferences("kesenhoo", Context.MODE_PRIVATE);

//下面的包名应该是需要访问的其他程序的所在位置,并设置为忽视安全问题

//下面的包名应该是需要访问的其他程序的所在位置,并设置为忽视安全问题 Context otherContext = getContext().createPackageContext("com.hoo.preferences", Context.CONTEXT_IGNORE_SECURITY); SharedPreferences preferences = otherContext.getSharedPreferences("kesenhoo", Context.MODE_PRIVATE); String name = preferences.getString("name", ""); int age = preferences.getInt("age", 20); Log.i(TAG, "name="+ name + ",age="+ age);


6.GetPreferences()与getSharedPreferences()的区别

To get aSharedPreferencesobject 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.

官方Demo:

public class Calc extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); // We need an Editor object to make preference changes. // All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); } }



相关文章:

  • Servlet的生命周期
  • 【Android】数据存储之SQLite
  • Linux 超级用户和普通用户切换命令
  • Android数据存储SQLite - 事务操作
  • 第二阶段冲刺--团队站立会议05
  • Android数据显示之ListView
  • String、StringBuffer、StringBuilder比较
  • Android数据存储之ContentProvider
  • [BZOJ 3680]吊打XXX(模拟退火)
  • 可达性分析算法-确定那些对象是垃圾(转)
  • Android之使用ContentResolver对通信录中的数据进行简单操作
  • Android之网络操作 - 从网络获取图片或网页
  • OpenGL学习--开发环境
  • jQuery常用总结(转载)
  • Android之把从网络中获取的数据以XML与Json格式返回
  • 收藏网友的 源程序下载网
  • avalon2.2的VM生成过程
  • CentOS6 编译安装 redis-3.2.3
  • Django 博客开发教程 16 - 统计文章阅读量
  • ES6核心特性
  • extjs4学习之配置
  • Hexo+码云+git快速搭建免费的静态Blog
  • Java,console输出实时的转向GUI textbox
  • Just for fun——迅速写完快速排序
  • mysql 数据库四种事务隔离级别
  • Python学习笔记 字符串拼接
  • React16时代,该用什么姿势写 React ?
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • Vue 2.3、2.4 知识点小结
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 服务器之间,相同帐号,实现免密钥登录
  • 给Prometheus造假数据的方法
  • 诡异!React stopPropagation失灵
  • 聚类分析——Kmeans
  • 力扣(LeetCode)22
  • 区块链共识机制优缺点对比都是什么
  • 深入 Nginx 之配置篇
  • 使用 Docker 部署 Spring Boot项目
  • 推荐一个React的管理后台框架
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • #Linux(权限管理)
  • $.ajax()方法详解
  • (003)SlickEdit Unity的补全
  • (function(){})()的分步解析
  • (Ruby)Ubuntu12.04安装Rails环境
  • (第二周)效能测试
  • (第一天)包装对象、作用域、创建对象
  • (翻译)terry crowley: 写给程序员
  • (非本人原创)我们工作到底是为了什么?​——HP大中华区总裁孙振耀退休感言(r4笔记第60天)...
  • (附源码)ssm高校实验室 毕业设计 800008
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910