And*_*rew 456 android sharedpreferences
如何删除应用程序的SharedPreferences数据?
我正在创建一个使用大量Web服务来同步数据的应用程序.出于测试目的,我需要在重新启动应用程序时清除一些SharedPreferences值.
Mar*_*k B 837
要删除特定值:SharedPreferences.Editor.remove()后跟一个commit()
删除所有SharedPreferences.Editor.clear()
后跟一个commit()
如果您不关心返回值并且从应用程序的主线程中使用它,请考虑使用apply()
.
小智 162
我的解决方案
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("text").commit();
Run Code Online (Sandbox Code Playgroud)
小智 118
删除所有首选项:
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().clear().commit();
Run Code Online (Sandbox Code Playgroud)
删除单个首选项:
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().remove("KeyName").commit();
Run Code Online (Sandbox Code Playgroud)
Nob*_*obu 57
如果不必每次都删除它,您可以从以下位置手动删除它:
设置 - >应用程序 - >管理应用程序 - >(选择您的应用程序) - >清除数据或卸载
较新版本的Android:
设置 - >应用程序 - >(选择您的应用程序) - >存储 - >清除数据和清除缓存
rub*_*com 23
在一行中删除Android共享首选项:-)
context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();
Run Code Online (Sandbox Code Playgroud)
或者apply
对于非阻塞异步操作:
this.getSharedPreferences("YOUR_PREFS", 0).edit().clear().apply();
Run Code Online (Sandbox Code Playgroud)
小智 18
似乎所有解决方案都没有完全正常工作或死亡
清除活动中的所有SharedPreferences
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).
edit().clear().apply();
Run Code Online (Sandbox Code Playgroud)
在onCreate之后从主活动中调用此方法
注意*我用过
.apply()
Run Code Online (Sandbox Code Playgroud)
代替
.commit()
Run Code Online (Sandbox Code Playgroud)
你可以自由选择commit();
Joh*_*ong 11
即使没有root电话,您也可以使用adb shell执行此操作.唯一的问题是app必须是可调试的.
run-as <your package name> <command>
Run Code Online (Sandbox Code Playgroud)
例如:
run-as com.asdf.blah rm /data/data/com.asdf.blah/databases/myDB.db
Run Code Online (Sandbox Code Playgroud)
或者,您可以执行上述操作,但不使用将指导您到应用程序包根目录的命令,并允许您在应用程序的上下文中执行更多命令.
Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Run Code Online (Sandbox Code Playgroud)
对于 Kotlin 用户来说这相当简单:
val sharedPref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
sharedPref.edit().clear().apply()
Run Code Online (Sandbox Code Playgroud)
清除所有:
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply()
Run Code Online (Sandbox Code Playgroud)
在类定义中
private static final String PREFERENCES = "shared_prefs";
private static final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(PREFERENCES, MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)
在课堂上
public static void deleteAllSharePrefs(){
sharedPreferences.edit().clear().commit();
}
Run Code Online (Sandbox Code Playgroud)
从API 24(Nougat)开始,你可以这样做:
context.deleteSharedPreferences("YOUR_PREFS");
Run Code Online (Sandbox Code Playgroud)
但是,没有向后兼容性,所以如果你支持少于24的东西,坚持:
context.getSharedPreferences("YOUR_PREFS", Context.MODE_PRIVATE).edit().clear().apply();
Run Code Online (Sandbox Code Playgroud)
试试这段代码:
SharedPreferences sharedPreferences = getSharedPreferences("fake", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.clear().commit();
Run Code Online (Sandbox Code Playgroud)
您始终可以按照此处其他答案的建议以编程方式执行此操作。但出于开发目的,我发现这Plugin
非常有帮助,因为它显着加快了我的开发速度。
插件:亚行的想法
它为您提供了从 Android Studio 本身清除应用程序数据和撤销权限的功能,只需单击一个按钮。
要从首选项中删除键值对,您可以轻松执行以下操作
getActivity().getSharedPreference().edit().remove("key").apply();
Run Code Online (Sandbox Code Playgroud)
我还开发了一个库,可以轻松操作共享首选项。您可能会找到以下链接
https://github.com/farruhha/SimplePrefs
String prefTag = "someTag";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
prefs.edit().remove(prefTag).commit();
Run Code Online (Sandbox Code Playgroud)
这将删除已保存的名为“someTag”的共享首选项。
归档时间: |
|
查看次数: |
349333 次 |
最近记录: |