如何删除共享偏好,而在Android应用程序卸载

我有一个Android应用程序通过SharedPreferences保存login详细信息,如用户名和密码,这很好,但我需要删除所有我的应用程序卸载时使用的SharedPreferences 。 怎么做?

 SavePreferences("one ", ""); SavePreferences("two", ""); LoadPreferences(); private void SavePreferences(String key, String value){ sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, value); editor.commit(); } private void LoadPreferences(){ sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE); String strSavedMem1 = sharedPreferences.getString("MEM1", ""); String strSavedMem2 = sharedPreferences.getString("MEM2", ""); } 

我想在我的应用程序卸载时删除这个SharedPreferences

问题不在于偏好。 这是备份pipe理器 ! ..因为android-23默认备份作为任务存储应用程序的数据,包括偏好到云。 稍后当你卸载然后安装较新的版本,你可能会使用恢复的首选项。 为了避免这种情况,只需将其添加到您的清单(或至lessdebugging清单):

 <application ... android:allowBackup="false"> ... </application> 

阅读: http : //developer.android.com/guide/topics/data/backup.html

你也会看到,如果你在Android > Lint > Security下运行Lint:

皮棉警告备份

在这里提到备份的过程就像是一个黑匣子,这是很好的。你不知道什么时候开始,以及检查之间的时期…所以更好的开发,以禁用它。

或者,您可以在卸载应用程序之前清除caching。

我希望这可以帮助。

随着应用程序卸载, SharedPreferences总是被删除。

卸载任何应用程序时,应用程序在内部存储器中所做的所有更改都将被撤消,这意味着您的SharedPreference文件,其他数据文件,数据库文件,应用程序会被Android操作系统自动删除。

编辑:29/04/15:> = 21 API请参阅@Maher Abuthraa的答案

它奇怪,但我发现解决scheme如下:

  1. Manifest.xml文件的清单标记中添加xmlns:tools="http://schemas.android.com/tools"
  2. Manifest.xml文件的应用标签中添加android:allowBackup="false"
  3. Manifest.xml文件的应用程序标签中添加tools:replace="android:allowBackup"

Manifest.xml文件应该看起来像这样。

  <?xml version="1.0" encoding="utf-8"?><!--suppress ALL --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.package"> // Other code <application android:name="com.package.Application" android:allowBackup="false" android:hardwareAccelerated="true" android:icon="@drawable/appicon" android:label="@string/application_name" android:largeHeap="true" android:theme="@style/AppTheme" tools:replace="android:allowBackup"> <activity android:name="com.package.SplashActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/application_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> // Other code </application> </manifest> 

完成。

设置allowBackup =“false”将从备份和恢复中select一个应用程序。