Can Shared Preferences be private?

怎甘沉沦 提交于 2019-12-30 05:41:11

问题


From the name alone, I am guessing that Shared Preferences are... shared among apps? That is, even if my app defines and creates them, any app on android can access them. Is this correct?

(If this isn't correct, then why does the Data Storage Dev Guide emphasize Internal Storage as "Store private data"?)

If this is correct, can I modify this default behavior so that a preference is only visible from the app in which I define and create it?

If so, how do I do that?


回答1:


If i'm correct you can make store it as private by calling

getSharedPreferences(yourfile, MODE_PRIVATE);


public static final int MODE_PRIVATE 

Since: API Level 1 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:


SharedPreferences are private by default. They are shared among the components of your app.




回答3:


The getSharedPreferences in the Context class takes two arguments, String name and int mode. Mode determines if the shared preferences are private or not. Context.getSharedPreferences



来源:https://stackoverflow.com/questions/5929735/can-shared-preferences-be-private

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!