下面列出了android.content.SharedPreferences.Editor#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Record the precache event. The event is persisted in shared preferences if the native library
* is not loaded. If library is loaded, the event will be recorded as UMA metric, and any prior
* persisted events are recorded to UMA as well.
* @param event the precache event.
*/
public static void record(int event) {
SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences();
long persistent_metric = sharedPreferences.getLong(PREF_PERSISTENCE_METRICS, 0);
Editor preferencesEditor = sharedPreferences.edit();
if (LibraryLoader.isInitialized()) {
RecordHistogram.recordEnumeratedHistogram(
EVENTS_HISTOGRAM, Event.getBitPosition(event), Event.EVENT_END);
for (int e : Event.getEventsFromBitMask(persistent_metric)) {
RecordHistogram.recordEnumeratedHistogram(
EVENTS_HISTOGRAM, Event.getBitPosition(e), Event.EVENT_END);
}
preferencesEditor.remove(PREF_PERSISTENCE_METRICS);
} else {
// Save the metric in preferences.
persistent_metric = Event.addEventToBitMask(persistent_metric, event);
preferencesEditor.putLong(PREF_PERSISTENCE_METRICS, persistent_metric);
}
preferencesEditor.apply();
}
/**
* Changes to use one single pref as dump event bit mask instead of having one pref per event
* type.
*/
private void remapDumpEventPref() {
Resources resources = service.getResources();
int[] eventTypes = AccessibilityEventUtils.getAllEventTypes();
int eventDumpMask = 0;
Editor editor = sharedPreferences.edit();
for (int eventType : eventTypes) {
String prefKey = resources.getString(R.string.pref_dump_event_key_prefix, eventType);
if (sharedPreferences.getBoolean(prefKey, false)) {
eventDumpMask |= eventType;
}
editor.remove(prefKey);
}
if (eventDumpMask != 0) {
editor.putInt(resources.getString(R.string.pref_dump_event_mask_key), eventDumpMask);
}
editor.apply();
}
/**
* Remove the database from the saved preferences
*
* @param database
* @param preserveOverlays
*/
private void removeDatabaseFromPreferences(String database, boolean preserveOverlays) {
Editor editor = settings.edit();
Set<String> databases = settings.getStringSet(databasePreference,
new HashSet<String>());
if (databases != null && databases.contains(database)) {
Set<String> newDatabases = new HashSet<String>();
newDatabases.addAll(databases);
newDatabases.remove(database);
editor.putStringSet(databasePreference, newDatabases);
}
editor.remove(getTileTablesPreferenceKey(database));
editor.remove(getFeatureTablesPreferenceKey(database));
if (!preserveOverlays) {
editor.remove(getFeatureOverlayTablesPreferenceKey(database));
deleteTableFiles(database);
}
editor.commit();
}
public static void remove(Context context, String key) {
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
Editor preferencesEditor = preferences.edit();
preferencesEditor.remove(key);
if (!preferencesEditor.commit()) throw new AssertionError("failed to remove identity key/value to shared preferences");
}
/**
* 删除键值对
*/
public static void removeKey(String preName, Context context, String key) {
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
Editor editor = pre.edit();
editor.remove(key);
editor.commit();
}
/**
* 批量移除喜好配置中的keys
* @param spFileName
* @param spKeys
*/
public boolean batchRemoveKeys(String spFileName, String... spKeys) {
if (spKeys != null && spKeys.length > 0) {
Editor spFileEditor = getEditor(spFileName);
for (String toRemoveKey : spKeys) {
spFileEditor.remove(toRemoveKey);
}
return spFileEditor.commit();
}
return false;
}
/**
* @param key The name of the preference to modify.
* @param value The new value for the preference.
* @see Editor#putStringSet(String, Set)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void putStringSet(final String key, final Set<String> value) {
final Editor editor = getPreferences().edit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
editor.putStringSet(key, value);
} else {
// Workaround for pre-HC's lack of StringSets
int stringSetLength = 0;
if (mPrefs.contains(key + LENGTH)) {
// First read what the value was
stringSetLength = mPrefs.getInt(key + LENGTH, -1);
}
editor.putInt(key + LENGTH, value.size());
int i = 0;
for (String aValue : value) {
editor.putString(key + "[" + i + "]", aValue);
i++;
}
for (; i < stringSetLength; i++) {
// Remove any remaining values
editor.remove(key + "[" + i + "]");
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
}
/**
* 从文件中删除数据
*
* @param context
* @param key
*/
public static void deleteData(Context context, String key) {
SharedPreferences sharedPreferences = context
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.remove(key);
editor.apply();
}
public static void remove(String... keys) {
if (keys != null) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
LeisureReadApp.getAppContext());
Editor editor = sharedPreferences.edit();
for (String key : keys) {
editor.remove(key);
}
editor.commit();
}
}
public void fixPersistentSettings() {
final String badDebugName = "DEBUG_MODE\"";
final String badNotificationName = "NOTFICATION_ICON";
final String badLockScreenName = "LOCK_SCREEN\"";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Map<String, ?> prefNames = prefs.getAll();
// Don't do anything if the bad preferences have already been fixed.
if (!prefNames.containsKey(badDebugName) &&
!prefNames.containsKey(badNotificationName) &&
!prefNames.containsKey(badLockScreenName)) {
return;
}
Editor editor = prefs.edit();
if (prefNames.containsKey(badDebugName)) {
editor.putString(AppSettings.DEBUG_MODE, prefs.getString(badDebugName, null));
editor.remove(badDebugName);
}
if (prefNames.containsKey(badNotificationName)){
editor.putBoolean(AppSettings.NOTIFICATION_ICON, prefs.getBoolean(badNotificationName, true));
editor.remove(badNotificationName);
}
if (prefNames.containsKey(badLockScreenName)) {
editor.putString(AppSettings.LOCK_SCREEN, prefs.getString(badLockScreenName, null));
editor.remove(badLockScreenName);
}
editor.apply();
}
public void disconnect() {
Log.d(LOGTAG, "disconnect()...");
if (sharedPrefs.contains(Constants.XMPP_LOGGEDIN)) {
Editor editor = sharedPrefs.edit();
editor.remove(Constants.XMPP_LOGGEDIN);
editor.remove(Constants.XMPP_REGISTERED);
editor.apply();
}
terminatePersistentConnection();
}
public static void removePreference(Context context, String key){
SharedPreferences mainPref =
context.getSharedPreferences(context.getResources()
.getString(R.string.shared_pref_package),
Context.MODE_PRIVATE
);
if (mainPref.contains(key)) {
Editor editor = mainPref.edit();
editor.remove(key);
editor.commit();
}
}
public void remove(String key) {
Editor editor = mPref.edit();
editor.remove(key);
editor.apply();
}
public void setSettings() {
// apiKey = getMetaDataValue("ANDROIDPN_API_KEY");
// Log.i(LOGTAG, "apiKey=" + apiKey);
// // if (apiKey == null) {
// // Log.e(LOGTAG, "Please set the androidpn api key in the manifest file.");
// // throw new RuntimeException();
// // }
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
apiKey = mySharedPreferences.getString("prefApikey", "1234567890").trim();
xmppHost = mySharedPreferences.getString("prefXmpphost", "192.168.0.1").trim();
xmppPort = mySharedPreferences.getString("prefXmppport", "5222").trim();
email = mySharedPreferences.getString("prefEmail", "").trim();
pass = mySharedPreferences.getString("prefPass", "").trim();
user = mySharedPreferences.getString("prefUser", "").trim();
name = mySharedPreferences.getString("prefName", "").trim();
boolean prefNtfy = mySharedPreferences.getBoolean("prefNtfy",true);
boolean prefSound = mySharedPreferences.getBoolean("prefSound",true);
boolean prefVibrate = mySharedPreferences.getBoolean("prefVibrate",true);
boolean prefToast = mySharedPreferences.getBoolean("prefToast",false);
Log.i(LOGTAG, "apiKey=" + apiKey);
Log.i(LOGTAG, "xmppHost=" + xmppHost);
Log.i(LOGTAG, "xmppPort=" + xmppPort);
Log.i(LOGTAG, "user=" + user);
Log.i(LOGTAG, "name=" + name);
Log.i(LOGTAG, "email=" + email);
sharedPrefs = context.getSharedPreferences(
Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putString(Constants.API_KEY, apiKey);
editor.putString(Constants.VERSION, version);
editor.putString(Constants.XMPP_HOST, xmppHost);
editor.putString(Constants.XMPP_USERNAME, user);
editor.putString(Constants.XMPP_PASSWORD, pass);
editor.putString(Constants.XMPP_EMAIL, email);
editor.putString(Constants.NAME, name);
try {
editor.remove(Constants.SETTINGS_NOTIFICATION_ENABLED);
editor.remove(Constants.SETTINGS_SOUND_ENABLED);
editor.remove(Constants.SETTINGS_VIBRATE_ENABLED);
editor.remove(Constants.SETTINGS_TOAST_ENABLED);
} catch (Exception e) {
Log.d(LOGTAG, "Settings not removed");
}
editor.putBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED, prefNtfy);
editor.putBoolean(Constants.SETTINGS_SOUND_ENABLED, prefSound);
editor.putBoolean(Constants.SETTINGS_VIBRATE_ENABLED, prefVibrate);
editor.putBoolean(Constants.SETTINGS_TOAST_ENABLED, prefToast);
editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort.trim()));
editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,
callbackActivityPackageName);
editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME,
callbackActivityClassName);
editor.apply();
// Log.i(LOGTAG, "sharedPrefs=" + sharedPrefs.toString());
}
public static boolean remove(String key)
{
Editor editor = edit();
editor.remove(key);
return editor.commit();
}
public static void remove(Context context, String key) {
SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
Editor edit = sharedPreferences.edit();
edit.remove(key);
edit.commit();
}
public static boolean removeTemp(String key)
{
Editor editor = editTemp();
editor.remove(key);
return editor.commit();
}
public static boolean remove(Context context, String fileName, String key) {
SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.remove(key);
return editor.commit();
}
public void remove(String key) {
Editor edit = sharedPreferences.edit();
edit.remove(key);
edit.apply();
}
public void clearToken() {
Editor editor = sharedPreferences.edit();
editor.remove(TOKEN_KEY);
editor.commit();
}