针对4.0版本及以上版本的sharedpreferences不能两工程间通信的解决办法
16lz
2022-08-06
通过查看官方文档https://developer.android.com/reference/android/content/Context.html
得出getSharedPreferences()两工程间共享数据在android2.3以后功能默认关闭了
所以查看源码
frameworks/base/core/java/android/app/ContextImpl.java
第605行
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {//---> 获取工程文件的targetSdkVersion大于HONEYCOMB就 关闭该功能了
// If somebody else (some other process) changed the prefs
// file behind our back, we reload it. This has been the
// historical (if undocumented) behavior.
sp.startReloadIfChangedUnexpectedly();
}
那么就去查看 android.os.Build.VERSION_CODES.HONEYCOMB的值
/home/ss/c1.5/android4.0/frameworks/base/core/java/android/os/Build.java
看到第243行
public static final int HONEYCOMB = 11; // android2.3的targetSdkVersion=10,这也就验证了官方文档写的不支持android4.0--targetSdkVersion=14
找到了原因,那么就有 解决办法:
修改 ContextImpl.java
将android.os.Build.VERSION_CODES.HONEYCOMB的位置替换为15即可,更高的版本,那么就用更大的的数字咯
注意:为什么不直接修改HONEYCOMB=15呢?因为在其他的地方有可能也有限制4.0及以上版本什么什么的操作,但是我现在又用不到,就不去影响了
更多相关文章
- android Dialog大小修改
- android“设置”里的版本号
- Android(安卓)version and Linux Kernel version
- opengrok setup on ubuntu for android source code browser
- 从 Android(安卓)Sample ApiDemos 中学习 android.animation API
- android源码下载方式
- 修改android系统和watchdog的延时
- Android——开发环境
- android通过ksoap2对webservice的解析