Can Shared Preferences be private?

Short answer

Yes

Long answer

Shared Preference are saved in the file system of your device, and the Shared Preferences Mode defines whether other applications have the right to read or write the Shared Preferences or not. Android SharedPreferences with mode “MODE_PRIVATE”, “MODE_WORLD_READABLE” and “MODE_WORLD_WRITABLE” are possible in android.

  • MODE_PRIVATE
    This is the default mode, where the created file can only be accessed by the calling the application (or other applications sharing the same user ID).
  • MODE_WORLD_READABLE
    This mode allows all other applications to have read access to the Shared Preferences.
  • MODE_WORLD_WRITEABLE
    If you choose this mode, all other applications to have write access to the Shared Preferences.

The getSharedPreferences mothod in the Context class can take two arguments. The first argument is the name of the Shared Preference and the second on is the mode. With the mode you can determines if the shared preferences are private or not.

About Shared Preferences

Full name
android.content.SharedPreferences

Description
Interface for accessing and modifying preference data. For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through the SharedPreferences.Editor.

Note
Currently SharedPreferences do not support multiple processes.

Nested Classes

  • SharedPreferences.Editor
    Interface used for modifying values in a SharedPreferences object.
  • SharedPreferences.OnSharedPreferenceChangeListener
    Interface definition for a callback to be invoked when a shared preference is changed.

Public Methods

  • abstract Boolean contains(String key)
    Checks whether the preferences contains a preference.
  • SharedPreferences.Editor edit()
    Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.
  • Map getAll()
    Retrieve all values from the preferences.
  • boolean getBoolean(String key, boolean defValue)
    Retrieve a boolean value from the preferences.
  • float getFloat(String key, float defValue)
    Retrieve a float value from the preferences.
  • int getInt(String key, int defValue)
    Retrieve an int value from the preferences.
  • long getLong(String key, long defValue)
    Retrieve a long value from the preferences.
  • String getString(String key, String defValue)
    Retrieve a String value from the preferences.
  • Set getStringSet(String key, Set defValues)
    Retrieve a set of String values from the preferences.
  • void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
    Registers a callback to be invoked when a change happens to a preference.
  • void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
    Unregisters a previous callback.

Summary
Android SharedPreferences with private mode are default in android. Other modes like WORLD_READABLE and WORLD_WRITABLE are possible if you need SharedPreferences with this mode.