网上的文章不要全信哦,也不要乱写,误人子弟
1.我看网上很多文章说SharedPreferances中存储的数据在不同包的Activity中无法读取。他指的是Context.MODE_PRIVATE这种模式下。
但是我测试了一下,在不同包的Activity中是可以读取SharedPreferances中的数据的。(Context.MODE_PRIVATE模式下 )
而且在官方的API文章中明确指出
Context类
public static final int MODE_PRIVATE
File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
See Also
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
Constant Value: 0 (0x00000000)
2.不仅在不同包的Activity中可以读取SharedPreferances中的数据。不同应用也可以读取SharedPreferances中的数据
public static final int MODE_WORLD_READABLE
File creation mode: allow all other applications to have read access to the created file.
See Also
MODE_PRIVATE
MODE_WORLD_WRITEABLE
Constant Value: 1 (0x00000001)
public static final int MODE_WORLD_WRITEABLE
File creation mode: allow all other applications to have write access to the created file.
See Also
MODE_PRIVATE
MODE_WORLD_READABLE
Constant Value: 2 (0x00000002)
3.最后,SharedPreferances的本质就是xml文档,放在/data/data/<package name>/shares_prefs这个文件夹下