下面列出了android.content.SharedPreferences.Editor#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void removeAccessToken(Context ctx, String hostPackage,
String accessToken) {
SharedPreferences prefs = getPrefsForHost(ctx, hostPackage);
Log.d(_tag, "removing AccessToken.");
if (prefs.getString(PREF_KEY_TOKEN, "").equals(accessToken))
{
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
SharedPreferences hostPrefs = ctx.getSharedPreferences("KP2A.PluginAccess.hosts", Context.MODE_PRIVATE);
if (hostPrefs.contains(hostPackage))
{
hostPrefs.edit().remove(hostPackage).commit();
}
}
public boolean removeAll() {
Editor prefsWriter = this.cookiePrefs.edit();
prefsWriter.clear();
prefsWriter.commit();
this.cookies.clear();
return true;
}
@Override
protected void onPause() {
super.onPause();
// save the current directory
Editor editor = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE).edit();
editor.clear();
if (this.currentDirectory != null) {
editor.putString(CURRENT_DIRECTORY, this.currentDirectory.getAbsolutePath());
}
editor.commit();
}
public void testExportImportPreferences() throws Exception {
// Populate with some initial values
Editor editor = preferences.edit();
editor.clear();
editor.putBoolean("bool1", true);
editor.putBoolean("bool2", false);
editor.putFloat("flt1", 3.14f);
editor.putInt("int1", 42);
editor.putLong("long1", 123456789L);
editor.putString("str1", "lolcat");
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
// Export it
byte[] exported = preferenceBackupHelper.exportPreferences(preferences);
// Mess with the previous values
editor = preferences.edit();
editor.clear();
editor.putString("str2", "Shouldn't be there after restore");
editor.putBoolean("bool2", true);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
// Import it back
preferenceBackupHelper.importPreferences(exported, preferences);
assertFalse(preferences.contains("str2"));
assertTrue(preferences.getBoolean("bool1", false));
assertFalse(preferences.getBoolean("bool2", true));
assertEquals(3.14f, preferences.getFloat("flt1", 0.0f));
assertEquals(42, preferences.getInt("int1", 0));
assertEquals(123456789L, preferences.getLong("long1", 0));
assertEquals("lolcat", preferences.getString("str1", ""));
}
public static void clearPreferences(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor prefEdit = prefs.edit();
prefEdit.clear();
boolean cleared = prefEdit.commit();
if (cleared) {
Timber.d("clearPreferences(): Preferences cleared");
Toast.makeText(context, "Preferences cleared", Toast.LENGTH_LONG).show();
} else {
Timber.e("clearPreferences(): Failed to clear preferences");
}
}
/**
* 清空 SharedPreferences 中 Token信息。
*
* @param context 应用程序上下文环境
*/
public static void clear(Context context) {
if (null == context) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.clear();
editor.commit();
}
public static void clearPreference(Context context,
final SharedPreferences p) {
final Editor editor = p.edit();
editor.clear();
editor.commit();
}
/**
* Imports all preferences from the given stream.
*
* @param reader the stream to read from
* @param preferences the shared preferences to edit
* @throws IOException if there are any errors while reading
*/
@SuppressLint("CommitPrefEdits")
public void importPreferences(DataInputStream reader, SharedPreferences preferences)
throws IOException {
Editor editor = preferences.edit();
editor.clear();
int numPreferences = reader.readInt();
for (int i = 0; i < numPreferences; i++) {
String name = reader.readUTF();
byte typeId = reader.readByte();
readAndSetPreference(name, typeId, reader, editor);
}
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
}
/**
* 清空 SharedPreferences 中 Token信息。
*
* @param context 应用程序上下文环境
*/
public static void clear(Context context) {
if (null == context) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.clear();
editor.commit();
}
public void saveAppMoveStatus() {
SharedPreferences mSharedPreferences = thisActivity.getSharedPreferences("app_order", Context.MODE_PRIVATE);
Editor editor = mSharedPreferences.edit();
editor.clear();
for (int i = 0; i < mAppInfos.size(); i++) {
editor.putInt(mAppInfos.get(i).mAppName, i);
}
editor.apply();
}
public void clear(){
Editor ed = preference.edit();
ed.clear();
ed.apply();
}
public static void clearPreference(Context context, final SharedPreferences p) {
final Editor editor = p.edit();
editor.clear();
editor.commit();
}
public void clear() {
Editor editor = mPref.edit();
editor.clear();
editor.apply();
}
private void deleteAllOldPreferences() {
Editor editor = mOldPrefs.edit();
editor.clear();
editor.commit();
}
public static void clear(Context context, String spName) {
SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.clear();
editSubmit(editor);
}
public static void clear(Context context) {
Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}
public static void clear(Context context, String fileName) {
Editor editor = context.getSharedPreferences(fileName, 0).edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}
public void clear() {
Editor editor = mPref.edit();
editor.clear();
editor.apply();
}
public static void clearPreference(Context context,
final SharedPreferences p) {
final Editor editor = p.edit();
editor.clear();
editor.commit();
}
public void clear() {
Editor editor = ctx.getSharedPreferences(PREFERENCES_FILE, Activity.MODE_PRIVATE).edit();
editor.clear();
editor.apply();
}