Android SharedPreferences with MODE_PRIVATE,MODE_WORLD_READABLE,MODE_WORLD_WRITABLE

use*_*789 35 android

SharedPreferences in Android are local to an Application, and not shared between different applications. When I say

SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_WORLD_READABLE);
Run Code Online (Sandbox Code Playgroud)

What does it signify to make this preferences MODE_WORLD_READABLE, MODE_WORLD_WRITABLE or MODE_PRIVATE?

Adr*_* C. 40

getSharedPreferences(String name, int mode)在这里解释

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).

MODE_WORLD_READABLE: File creation mode: allow all other applications to have read access to the created file.

MODE_WORLD_WRITEABLE : File creation mode: allow all other applications to have write access to the created file.
Run Code Online (Sandbox Code Playgroud)

更多信息在这里

编辑作为API 17中,MODE_WORLD_READABLE并且MODE_WORLD_WRITEABLE被弃用:

在API级别17中不推荐使用此常量.
创建世界可读文件非常危险,并且可能会在应用程序中造成安全漏洞.强烈劝阻; 相反,应用程序应该使用的相互作用,如更正式的机制ContentProvider,BroadcastReceiver以及Service.无法保证此访问模式将保留在文件中,例如在进行备份和还原时.


Anu*_*kur 13

首选项存储在文件系统中.该模式定义了谁有权访问您应用的首选项.

简单来说:

  • MODE_PRIVATE是首选项的操作模式.它是默认模式,表示只有调用的应用程序才能访问创建的文件.
  • 在MODE_WORLD_READABLE其他应用程序中可以读取创建的文件但不能修改它.
  • 如果MODE_WORLD_WRITEABLE其他应用程序也具有创建文件的写权限.

建议的方法是使用默认模式,而不指定文件名

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Run Code Online (Sandbox Code Playgroud)


Pra*_*gar 9

共享首选项也保存在文件系统的文件中.这些模式定义了其他应用程序是否有权读取文件中的分片首选项.

MODE_PRIVATE 表示只有创建共享首选项的应用程序才能读取写入首选项

MODE_WORLD_READABLE 表示其他应用程序也可以使用共享首选项API读取这些首选项,但不能在此首选项文件中调用值

MODE_WORLD_WRITEABLE 表示其他应用程序也可以使用共享首选项API在首选项文件中读取和写入

Following link also has some good explanation and code example to explain the MODES:

http://thedevelopersinfo.com/2009/11/25/getting-sharedpreferences-from-other-application-in-android/

http://chrisrisner.com/Accessing-the-Shared-Preferences-of-a-Different-Application-in-Android


Nar*_*esh 5

MODE_PRIVATE 表示您的应用程序创建的文件,用于存储只能由您自己的应用程序访问的应用程序首选项数据。

没有其他应用程序能够访问该文件。