如何从 Android 中的 App 中删除共享首选项数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3687315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete shared preferences data from App in Android
提问by Andrew
How do I delete SharedPreferences data for my application?
如何删除应用程序的 SharedPreferences 数据?
I'm creating an application that uses a lot of web services to sync data. For testing purposes, I need to wipe out some SharedPreferences values when I restart the app.
我正在创建一个使用大量 Web 服务来同步数据的应用程序。出于测试目的,我需要在重新启动应用程序时清除一些 SharedPreferences 值。
回答by Mark B
To remove specific values: SharedPreferences.Editor.remove()followed by a commit()
删除特定值:SharedPreferences.Editor.remove()后跟commit()
To remove them all SharedPreferences.Editor.clear()
followed by a commit()
将它们全部删除,SharedPreferences.Editor.clear()
然后是commit()
If you don't care about the return value and you're using this from your application's main thread, consider using apply()
instead.
如果您不关心返回值并且正在从应用程序的主线程使用它,请考虑使用它apply()
。
回答by vaibhav vijay
My solution:
我的解决方案:
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("text").commit();
回答by zserghei
Removing all preferences:
删除所有首选项:
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().clear().commit();
Removing single preference:
删除单一偏好:
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().remove("KeyName").commit();
回答by Nobu
If it's not necessary to be removed every time, you can remove it manually from:
如果不需要每次都删除,可以手动删除:
Settings -> Applications -> Manage applications -> (choose your app) -> Clear data or Uninstall
设置 -> 应用程序 -> 管理应用程序 ->(选择您的应用程序)-> 清除数据或卸载
Newer versions of Android:
较新版本的安卓:
Settings -> Applications -> (choose your app) -> Storage -> Clear data and Clear cache
设置 -> 应用程序 ->(选择你的应用) -> 存储 -> 清除数据和清除缓存
回答by rubdottocom
Deleting Android Shared Preferences in one line :-)
在一行中删除 Android 共享首选项 :-)
context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();
Or apply
for non-blocking asynchronous operation:
或者apply
对于非阻塞异步操作:
this.getSharedPreferences("YOUR_PREFS", 0).edit().clear().apply();
回答by Shady Keshk
Seems that all solution is not completely working or out-dead
似乎所有解决方案都不是完全有效或无效
to clear all SharedPreferences in an Activity
清除活动中的所有 SharedPreferences
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();
Call this from the Main Activity after onCreate
在 onCreate 之后从 Main Activity 调用它
note* i used .apply()
instead of .commit()
, you are free to choose commit();
注意*我用了.apply()
代替.commit()
,你可以自由选择commit();
回答by Johnson Wong
You can use the adb shell to do this even without a rooted phone. The only catch is that the app must be debuggable.
即使没有root手机,您也可以使用 adb shell 来执行此操作。唯一的问题是应用程序必须是可调试的。
run-as <your package name> <command>
For example:
例如:
run-as com.asdf.blah rm /data/data/com.asdf.blah/databases/myDB.db
Alternatively, you can just do the above but without the command which will direct you to the app package root and allow you to execute more commands in the app's context.
或者,您可以只执行上述操作,但不使用将引导您到应用程序包根目录并允许您在应用程序上下文中执行更多命令的命令。
回答by Vaishali Sutariya
Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
回答by Dan Alboteanu
Clear them all:
清除它们:
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply()
回答by afathman
As of API 24 (Nougat) you can just do:
从 API 24 (Nougat) 开始,您可以执行以下操作:
context.deleteSharedPreferences("YOUR_PREFS");
However, there is no backward compatibility, so if you're supporting anything less than 24, stick with:
但是,没有向后兼容性,因此如果您支持小于 24 的任何内容,请坚持使用:
context.getSharedPreferences("YOUR_PREFS", Context.MODE_PRIVATE).edit().clear().apply();