2013년 2월 27일 수요일

SharedPreference의 사용범위(access range)

결과 부터 말하면, 사용범위는 one-process(MODE_PRIVATE에 의해)와 multi-process(MODE_MULTI_PROCESS에 의해) 입니다.

참고 사항

  1. MODE_MULTI_PROCESS는 Gingerbread(Android2.3)까지는 always on이었으나, 이후 버전 부터는 기본적으로 off되었다고 하니, 여러개의 process에서 공유하려면, MODE_MULTI_PROCESS를 명시적으로 이용하여 getSharedPreferences()를 이용해야 겠네요.
  2. MODE_WORLD_READABLE과 MODE_WORLD_WRITEABLE은 위험해서 deprecated(API level17에서) 되었습니다.


public abstract SharedPreferences getSharedPreferences (String name, int mode)

Added in API level 1
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
Parameters
nameDesired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
modeOperating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. The bit MODE_MULTI_PROCESS can also be used if multiple processes are mutating the same SharedPreferences file. MODE_MULTI_PROCESS is always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions.
Returns
  • Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.




public static final int MODE_PRIVATE

Added in 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).
Constant Value: 0 (0x00000000)

public static final int MODE_WORLD_READABLE

Added in API level 1
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. File creation mode: allow all other applications to have read access to the created file.
Constant Value: 1 (0x00000001)

public static final int MODE_WORLD_WRITEABLE

Added in API level 1
This constant was deprecated in API level 17.
Creating world-writable 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. File creation mode: allow all other applications to have write access to the created file.
Constant Value: 2 (0x00000002)