如何删除应用程序的SharedPreferences数据?
我正在创建一个使用大量web服务来同步数据的应用程序。出于测试目的,我需要在重新启动应用程序时删除一些SharedPreferences值。
如何删除应用程序的SharedPreferences数据?
我正在创建一个使用大量web服务来同步数据的应用程序。出于测试目的,我需要在重新启动应用程序时删除一些SharedPreferences值。
当前回答
在类定义中:
private static final String PREFERENCES = "shared_prefs";
private static final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(PREFERENCES, MODE_PRIVATE);
类内部:
public static void deleteAllSharedPrefs(){
sharedPreferences.edit().clear().commit();
}
其他回答
你也可以用你的设备手动卸载你的应用。然后,当你重新安装应用程序时,共享首选项已被重置。
一行删除Android共享首选项:-)
context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();
或申请非阻塞异步操作:
this.getSharedPreferences("YOUR_PREFS", 0).edit().clear().apply();
String prefTag = "someTag";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
prefs.edit().remove(prefTag).commit();
这将删除保存的名称为“someTag”的共享首选项。
在类定义中:
private static final String PREFERENCES = "shared_prefs";
private static final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(PREFERENCES, MODE_PRIVATE);
类内部:
public static void deleteAllSharedPrefs(){
sharedPreferences.edit().clear().commit();
}
要删除特定的值:SharedPreferences.Editor.remove()后面跟着一个commit()
SharedPreferences.Editor.clear()后面跟着一个commit()
如果您不关心返回值,并且从应用程序的主线程中使用它,请考虑使用apply()。