跨进程SharedPreferences,createPackageContext生成的context实例缓存清除问题

我的主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


下面是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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值