跨进程SharedPreferences,createPackageContext生成的context实例缓存清除问题
2015-10-12 17:28
561 查看
我的主apk为A,调用的apk为B,
在A中通过createPackageContext(B.packagename,
INCLUDE_CODE)获得B的context实例,之后我通过B对文件数据修改,退出B,A中再通过context otherAppsContext = createPackageContext(B.packagename,
CONTEXT_IGNORE_SECURITY),otherAppsContext.getSharedPreferences("config",otherAppsContext.MODE_WORLD_READABLE);拿到的还是以前的数据。
解决办法,修改getsharedpreference("config",otherAppsContext.MODE_WORLD_READABLE)的权限MODE_WORLD_READABLE修改为 MODE_MULTI_PROCESS
下面是
SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all
writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.
This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.
在A中通过createPackageContext(B.packagename,
INCLUDE_CODE)获得B的context实例,之后我通过B对文件数据修改,退出B,A中再通过context otherAppsContext = createPackageContext(B.packagename,
CONTEXT_IGNORE_SECURITY),otherAppsContext.getSharedPreferences("config",otherAppsContext.MODE_WORLD_READABLE);拿到的还是以前的数据。
解决办法,修改getsharedpreference("config",otherAppsContext.MODE_WORLD_READABLE)的权限MODE_WORLD_READABLE修改为 MODE_MULTI_PROCESS
下面是
MODE_MULTI_PROCESS的说明:
SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all
writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.
This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.
相关文章推荐
- 动态拼接html
- JQuery中serialize()、serializeArray()和param()方法示例介绍
- Javascript hash functions to convert string into integer hash
- 移动前端头部标签(HTML5 head meta)转载
- js数组的操作
- Remove Nth Node From End of List
- jquery删除指定数组元素
- 最好的前端开发资源推荐(转载)
- Jquery-validate自定义校验方法
- 自制工具:迅速打开一个Node 环境的Playground
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- CSS工具(Autoprefixer、CSScomb、CSS3 Pie、CSS3 Maker)
- Jsoup解析网页源码时常用的Element(s)类
- jQuery-Tab切换
- 17.1.2.3 Determination of Safe and Unsafe Statements in Binary Logging
- Bootstrap 编码规范
- 关于Bootstrap 模态框的产生使用
- Newtonsoft.Json(Json.Net)学习笔记
- bootstrap 3 中表单元素的写法 ---- 重点是 input file 元素的
- Javascript闭包——懂不懂由你,反正我是懂了