user1730789
user1730789

Reputation: 5177

Android SharedPreferences with MODE_PRIVATE,MODE_WORLD_READABLE,MODE_WORLD_WRITABLE

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

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

Upvotes: 48

Views: 73627

Answers (4)

Naresh
Naresh

Reputation: 301

MODE_PRIVATE means the file which created by your app for storing app preferences data that can be only accessible to your own app only.

No other app would be able to access that file.

Upvotes: 5

Adrian C.
Adrian C.

Reputation: 1687

getSharedPreferences(String name, int mode) is explained here

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.

More info here

Edit As of API 17, the MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE are deprecated:

This constant was deprecated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore.

Upvotes: 56

Anup Cowkur
Anup Cowkur

Reputation: 20553

Preferences are stored in the file system. The mode defines who has access to your app's preferences.

In simple terms:

  • MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application.
  • In MODE_WORLD_READABLE other application can read the created file but can not modify it.
  • In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Upvotes: 18

Praful Bhatnagar
Praful Bhatnagar

Reputation: 7435

Shared preference are also saved in file in the file system. And these modes define whether other application would have the right to read the shard preference in the file or not.

MODE_PRIVATE means that only the application creating the shared preference can read write the preference

MODE_WORLD_READABLE means that other application can also read these preference using the shared preference API but can not wright value in this preference file

MODE_WORLD_WRITEABLE means that other application can also read and write in the preference file using the shared preference 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

Upvotes: 8

Related Questions