Many applications can provide a way to capture user settings in the settings of a specific application or activity. To support this, Android offers a simple set of APIs.
Preferences are usually pairs of name values. They can be stored as “General Preferences” in various actions in the application (note that at present they cannot be shared between processes). Or it may be something that needs to be preserved for a specific activity.
General preferences: General settings can be used by all components (actions, services, etc.) for applications.
Processed actions: these settings can only be used in action and cannot be used by other application components.
General settings:
getSharedPreferences Context. (1) (2), .
(1)
public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
MODE_PRIVATE - . , . : MODE_WORLD_READABLE MODE_WORLD_WRITEABLE. MODE_WORLD_READABLE , . MODE_WORLD_WRITEABLE .
(2) - PreferenceManager
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
, , :
int storedPreference = preferences.getInt("storedInt", 0);
SharedPreference.Editor . Editor - SharedPreference.
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference);
editor.commit();
, remove() clear(), .
:
. , . getPreferences() . getPreference getSharedPreferences() .
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);
, .
SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference);
editor.commit();
, . . Android android.preference. .
, .