SharedPreferences.java revision 02f4f0eb4919453e8dbf40549d4ae9c05f05b8dd
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.content; 18 19import java.util.Map; 20import java.util.Set; 21 22/** 23 * Interface for accessing and modifying preference data returned by {@link 24 * Context#getSharedPreferences}. For any particular set of preferences, 25 * there is a single instance of this class that all clients share. 26 * Modifications to the preferences must go through an {@link Editor} object 27 * to ensure the preference values remain in a consistent state and control 28 * when they are committed to storage. 29 * 30 * <p><em>Note: currently this class does not support use across multiple 31 * processes. This will be added later.</em> 32 * 33 * @see Context#getSharedPreferences 34 */ 35public interface SharedPreferences { 36 /** 37 * Interface definition for a callback to be invoked when a shared 38 * preference is changed. 39 */ 40 public interface OnSharedPreferenceChangeListener { 41 /** 42 * Called when a shared preference is changed, added, or removed. This 43 * may be called even if a preference is set to its existing value. 44 * 45 * <p>This callback will be run on your main thread. 46 * 47 * @param sharedPreferences The {@link SharedPreferences} that received 48 * the change. 49 * @param key The key of the preference that was changed, added, or 50 * removed. 51 */ 52 void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key); 53 } 54 55 /** 56 * Interface used for modifying values in a {@link SharedPreferences} 57 * object. All changes you make in an editor are batched, and not copied 58 * back to the original {@link SharedPreferences} until you call {@link #commit} 59 * or {@link #apply} 60 */ 61 public interface Editor { 62 /** 63 * Set a String value in the preferences editor, to be written back once 64 * {@link #commit} or {@link #apply} are called. 65 * 66 * @param key The name of the preference to modify. 67 * @param value The new value for the preference. 68 * 69 * @return Returns a reference to the same Editor object, so you can 70 * chain put calls together. 71 */ 72 Editor putString(String key, String value); 73 74 /** 75 * Set a set of String values in the preferences editor, to be written 76 * back once {@link #commit} is called. 77 * 78 * @param key The name of the preference to modify. 79 * @param values The new values for the preference. 80 * @return Returns a reference to the same Editor object, so you can 81 * chain put calls together. 82 */ 83 Editor putStringSet(String key, Set<String> values); 84 85 /** 86 * Set an int value in the preferences editor, to be written back once 87 * {@link #commit} or {@link #apply} are called. 88 * 89 * @param key The name of the preference to modify. 90 * @param value The new value for the preference. 91 * 92 * @return Returns a reference to the same Editor object, so you can 93 * chain put calls together. 94 */ 95 Editor putInt(String key, int value); 96 97 /** 98 * Set a long value in the preferences editor, to be written back once 99 * {@link #commit} or {@link #apply} are called. 100 * 101 * @param key The name of the preference to modify. 102 * @param value The new value for the preference. 103 * 104 * @return Returns a reference to the same Editor object, so you can 105 * chain put calls together. 106 */ 107 Editor putLong(String key, long value); 108 109 /** 110 * Set a float value in the preferences editor, to be written back once 111 * {@link #commit} or {@link #apply} are called. 112 * 113 * @param key The name of the preference to modify. 114 * @param value The new value for the preference. 115 * 116 * @return Returns a reference to the same Editor object, so you can 117 * chain put calls together. 118 */ 119 Editor putFloat(String key, float value); 120 121 /** 122 * Set a boolean value in the preferences editor, to be written back 123 * once {@link #commit} or {@link #apply} are called. 124 * 125 * @param key The name of the preference to modify. 126 * @param value The new value for the preference. 127 * 128 * @return Returns a reference to the same Editor object, so you can 129 * chain put calls together. 130 */ 131 Editor putBoolean(String key, boolean value); 132 133 /** 134 * Mark in the editor that a preference value should be removed, which 135 * will be done in the actual preferences once {@link #commit} is 136 * called. 137 * 138 * <p>Note that when committing back to the preferences, all removals 139 * are done first, regardless of whether you called remove before 140 * or after put methods on this editor. 141 * 142 * @param key The name of the preference to remove. 143 * 144 * @return Returns a reference to the same Editor object, so you can 145 * chain put calls together. 146 */ 147 Editor remove(String key); 148 149 /** 150 * Mark in the editor to remove <em>all</em> values from the 151 * preferences. Once commit is called, the only remaining preferences 152 * will be any that you have defined in this editor. 153 * 154 * <p>Note that when committing back to the preferences, the clear 155 * is done first, regardless of whether you called clear before 156 * or after put methods on this editor. 157 * 158 * @return Returns a reference to the same Editor object, so you can 159 * chain put calls together. 160 */ 161 Editor clear(); 162 163 /** 164 * Commit your preferences changes back from this Editor to the 165 * {@link SharedPreferences} object it is editing. This atomically 166 * performs the requested modifications, replacing whatever is currently 167 * in the SharedPreferences. 168 * 169 * <p>Note that when two editors are modifying preferences at the same 170 * time, the last one to call commit wins. 171 * 172 * <p>If you don't care about the return value and you're 173 * using this from your application's main thread, consider 174 * using {@link #apply} instead. 175 * 176 * @return Returns true if the new values were successfully written 177 * to persistent storage. 178 */ 179 boolean commit(); 180 181 /** 182 * Commit your preferences changes back from this Editor to the 183 * {@link SharedPreferences} object it is editing. This atomically 184 * performs the requested modifications, replacing whatever is currently 185 * in the SharedPreferences. 186 * 187 * <p>Note that when two editors are modifying preferences at the same 188 * time, the last one to call apply wins. 189 * 190 * <p>Unlike {@link #commit}, which writes its preferences out 191 * to persistent storage synchronously, {@link #apply} 192 * commits its changes to the in-memory 193 * {@link SharedPreferences} immediately but starts an 194 * asynchronous commit to disk and you won't be notified of 195 * any failures. If another editor on this 196 * {@link SharedPreferences} does a regular {@link #commit} 197 * while a {@link #apply} is still outstanding, the 198 * {@link #commit} will block until all async commits are 199 * completed as well as the commit itself. 200 * 201 * <p>If you call this from an {@link android.app.Activity}, 202 * the base class will wait for any async commits to finish in 203 * its {@link android.app.Activity#onPause}.</p> 204 */ 205 void apply(); 206 } 207 208 /** 209 * Retrieve all values from the preferences. 210 * 211 * @return Returns a map containing a list of pairs key/value representing 212 * the preferences. 213 * 214 * @throws NullPointerException 215 */ 216 Map<String, ?> getAll(); 217 218 /** 219 * Retrieve a String value from the preferences. 220 * 221 * @param key The name of the preference to retrieve. 222 * @param defValue Value to return if this preference does not exist. 223 * 224 * @return Returns the preference value if it exists, or defValue. Throws 225 * ClassCastException if there is a preference with this name that is not 226 * a String. 227 * 228 * @throws ClassCastException 229 */ 230 String getString(String key, String defValue); 231 232 /** 233 * Retrieve a set of String values from the preferences. 234 * 235 * @param key The name of the preference to retrieve. 236 * @param defValues Values to return if this preference does not exist. 237 * 238 * @return Returns the preference values if they exist, or defValues. 239 * Throws ClassCastException if there is a preference with this name 240 * that is not a Set. 241 * 242 * @throws ClassCastException 243 */ 244 Set<String> getStringSet(String key, Set<String> defValues); 245 246 /** 247 * Retrieve an int value from the preferences. 248 * 249 * @param key The name of the preference to retrieve. 250 * @param defValue Value to return if this preference does not exist. 251 * 252 * @return Returns the preference value if it exists, or defValue. Throws 253 * ClassCastException if there is a preference with this name that is not 254 * an int. 255 * 256 * @throws ClassCastException 257 */ 258 int getInt(String key, int defValue); 259 260 /** 261 * Retrieve a long value from the preferences. 262 * 263 * @param key The name of the preference to retrieve. 264 * @param defValue Value to return if this preference does not exist. 265 * 266 * @return Returns the preference value if it exists, or defValue. Throws 267 * ClassCastException if there is a preference with this name that is not 268 * a long. 269 * 270 * @throws ClassCastException 271 */ 272 long getLong(String key, long defValue); 273 274 /** 275 * Retrieve a float value from the preferences. 276 * 277 * @param key The name of the preference to retrieve. 278 * @param defValue Value to return if this preference does not exist. 279 * 280 * @return Returns the preference value if it exists, or defValue. Throws 281 * ClassCastException if there is a preference with this name that is not 282 * a float. 283 * 284 * @throws ClassCastException 285 */ 286 float getFloat(String key, float defValue); 287 288 /** 289 * Retrieve a boolean value from the preferences. 290 * 291 * @param key The name of the preference to retrieve. 292 * @param defValue Value to return if this preference does not exist. 293 * 294 * @return Returns the preference value if it exists, or defValue. Throws 295 * ClassCastException if there is a preference with this name that is not 296 * a boolean. 297 * 298 * @throws ClassCastException 299 */ 300 boolean getBoolean(String key, boolean defValue); 301 302 /** 303 * Checks whether the preferences contains a preference. 304 * 305 * @param key The name of the preference to check. 306 * @return Returns true if the preference exists in the preferences, 307 * otherwise false. 308 */ 309 boolean contains(String key); 310 311 /** 312 * Create a new Editor for these preferences, through which you can make 313 * modifications to the data in the preferences and atomically commit those 314 * changes back to the SharedPreferences object. 315 * 316 * <p>Note that you <em>must</em> call {@link Editor#commit} to have any 317 * changes you perform in the Editor actually show up in the 318 * SharedPreferences. 319 * 320 * @return Returns a new instance of the {@link Editor} interface, allowing 321 * you to modify the values in this SharedPreferences object. 322 */ 323 Editor edit(); 324 325 /** 326 * Registers a callback to be invoked when a change happens to a preference. 327 * 328 * @param listener The callback that will run. 329 * @see #unregisterOnSharedPreferenceChangeListener 330 */ 331 void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener); 332 333 /** 334 * Unregisters a previous callback. 335 * 336 * @param listener The callback that should be unregistered. 337 * @see #registerOnSharedPreferenceChangeListener 338 */ 339 void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener); 340} 341