我有一个Android应用程序通过SharedPreferences
保存登录详细信息,如用户名和密码,这可以正常工作,但我需要删除我的应用程序卸载时所有使用的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
。
问题不在于偏好。这是备用经理! ..因为android-23默认备份作为任务存储应用程序的数据,包括首选项到云。稍后当您卸载然后安装较新版本时,您可能会使用恢复的首选项。为避免这种情况,只需将其添加到清单(或至少调试清单):
<application ...
android:allowBackup="false">
...
</application>
阅读本文:http://developer.android.com/guide/topics/data/backup.html
如果你在Android > Lint > Security
下运行Lint,你也会看到:
这里提一下,备份过程就像一个黑盒子......你不知道它什么时候开始,以及检查之间的时间......所以最好开发禁用它。
====更新====
在将allowbackup
设置为false
之后,您可能会收到Manifest合并问题。要解决该问题,请添加:
tools:replace="android:allowBackup"
在应用程序元素中。感谢@ shahzain-ali
或者,您可以在卸载应用程序之前清除缓存。
我希望这可能有所帮助。
SharedPreferences
始终与应用程序卸载一起删除。
卸载任何应用程序时,应用程序在内部存储器中所做的所有更改都将被撤消,这意味着您的SharedPreference文件,其他数据文件,数据库文件,应用程序将被Android操作系统自动删除。
编辑:29/04/15:对于= = 21 API,请参阅@Maher Abuthraa 's answer
它很奇怪,但我通过以下方式找到了解决方案:
xmlns:tools="http://schemas.android.com/tools"
android:allowBackup="false"
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"
会选择备份和还原中的应用程序。
android:allowBackup="false"
问题不在于偏好。
使用此代码修复它..........
<application
android:allowBackup="true"
android:fullBackupContent="false"></application>