删除共享首选项

And*_*rew 456 android sharedpreferences

如何删除应用程序的SharedPreferences数据?

我正在创建一个使用大量Web服务来同步数据的应用程序.出于测试目的,我需要在重新启动应用程序时清除一些SharedPreferences值.

Mar*_*k B 837

要删除特定值:SharedPreferences.Editor.remove()后跟一个commit()

删除所有SharedPreferences.Editor.clear()后跟一个commit()

如果您不关心返回值并且从应用程序的主线程中使用它,请考虑使用apply().

  • context.getSharedPreferences("YOUR_PREFS",0).edit().clear().commit(); //删除所有你的prefs :) (261认同)
  • @rubdottocom更好地使用.apply()而不是.commit() (10认同)
  • 清除首选项文件似乎并未实际删除它.在我自己的测试中,通过观察"应用程序信息"面板中列出的"数据"用法,创建新的SharedPreference文件会为此值添加4KB,但使用editor.clear().commit()不会减少数量. (9认同)
  • @yoshi肯定有一个remove()以及一个clear().您使用remove()删除特定首选项,使用clear()删除所有首选项.最初的问题不清楚是否需要将它们全部删除. (2认同)
  • 在defaultSharedPreference()中不起作用 (2认同)
  • @rubdottocom 你为什么不把你的评论作为一个单独的答案? (2认同)

小智 162

我的解决方案

SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("text").commit();
Run Code Online (Sandbox Code Playgroud)

  • @ SiKni8是的,只删除键"/ text"的键/值对. (3认同)
  • 考虑使用apply()而不是commit()在后台线程中执行任务。 (3认同)
  • 这是否仅删除变量TEXT? (2认同)

小智 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)

  • 如何删除整个首选项文件,而不仅仅是文件内的首选项? (2认同)

Nob*_*obu 57

如果不必每次都删除它,您可以从以下位置手动删除它:

设置 - >应用程序 - >管理应用程序 - >(选择您的应用程序) - >清除数据或卸载

较新版本的Android:

设置 - >应用程序 - >(选择您的应用程序) - >存储 - >清除数据和清除缓存

  • 感谢您提供非程序化选项. (30认同)
  • 我们可以做这个程序化的事情. (4认同)
  • @amity-我们中有些人需要这个答案。我不确定如何在Android模拟器上删除SharedPrefs,以便继续测试我的代码。 (2认同)

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)

或者,您可以执行上述操作,但不使用将指导您到应用程序包根目录的命令,并允许您在应用程序的上下文中执行更多命令.


Vai*_*iya 9

Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Run Code Online (Sandbox Code Playgroud)


Sau*_*age 9

对于 Kotlin 用户来说这相当简单:

val sharedPref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
 sharedPref.edit().clear().apply()
Run Code Online (Sandbox Code Playgroud)


Dan*_*anu 8

清除所有:

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply()
Run Code Online (Sandbox Code Playgroud)


siv*_*ivi 7

在类定义中

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)


afa*_*man 7

从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)


Mub*_*hir 6

试试这段代码:

SharedPreferences sharedPreferences = getSharedPreferences("fake", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.clear().commit();
Run Code Online (Sandbox Code Playgroud)


Vis*_*ese 6

您始终可以按照此处其他答案的建议以编程方式执行此操作。但出于开发目的,我发现这Plugin非常有帮助,因为它显着加快了我的开发速度。

插件:亚行的想法

它为您提供了从 Android Studio 本身清除应用程序数据撤销权限的功能,只需单击一个按钮。

在此处输入图片说明


wiz*_*urd 5

您也可以使用设备手动卸载应用.然后,当您重新安装应用程序时,共享首选项已重置.


Pra*_*ash 5

如果是为了您的测试。您可以使用adb命令。

adb shell pm clear <package name>
Run Code Online (Sandbox Code Playgroud)


Far*_*aev 5

要从首选项中删除键值对,您可以轻松执行以下操作

getActivity().getSharedPreference().edit().remove("key").apply();
Run Code Online (Sandbox Code Playgroud)

我还开发了一个库,可以轻松操作共享首选项。您可能会找到以下链接

https://github.com/farruhha/SimplePrefs


Kir*_*k g 5

String prefTag = "someTag";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
prefs.edit().remove(prefTag).commit();
Run Code Online (Sandbox Code Playgroud)

这将删除已保存的名为“someTag”的共享首选项。