Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SharedPreferences with MODE_PRIVATE,MODE_WORLD_READABLE,MODE_WORLD_WRITABLE

Tags:

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

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

like image 940
user1730789 Avatar asked Oct 31 '12 07:10

user1730789


People also ask

What is Mode_private when creating shared preference file?

MODE_PRIVATE. By setting this mode, the file can only be accessed using calling application. 5. MODE_WORLD_READABLE. This mode allow other application to read the preferences.

What is Mode_private?

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.

Can we store HashMap in SharedPreferences?

This example demonstrates about How can I save a HashMap to Shared Preferences in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can we store objects in SharedPreferences?

We can store fields of any Object to shared preference by serializing the object to String.


2 Answers

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.

like image 72
Adrian C. Avatar answered Oct 13 '22 23:10

Adrian C.


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); 
like image 20
Anup Cowkur Avatar answered Oct 14 '22 01:10

Anup Cowkur