且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除Android中的所有sharedPreferences

更新时间:2023-01-06 12:22:23

要删除该应用程序的所有首选项,您可以使用:

  SharedPreferences.Editor.clear() 

此后,您必须 commit()删除.

因此,它看起来像完整的代码:

  getSharedPreferences(PREFS,0).edit().clear().commit()//将PREF更改为您的 

Is it possible to programmatically delete all sharedPreferences of an Android application, without knowing the file names? I mean all files, not all keys of a given file.

I'm running unit tests with instrumentation for several applications, and i need to clear every file the an app may create under /data/data/app.package.name/shared_prefs folder.

For example an app may create these files under shared_prefs:

appname.xml
app.package.name_preferences.xml
app.package.name_tips.xml

while for another app i could have:

app.package.name_prefs.xml
app.package.name_foo.xml

Note: Using pm clear YOUR_APP_PACKAGE the app crashes.

Thanks

To remove all prefs of the application you can use:

SharedPreferences.Editor.clear()

After that you have to commit() the removal.

So it would look like that in full code:

getSharedPreferences(PREFS, 0).edit().clear().commit() // change PREF to yours