shi*_*ena 99 android android-preferences
我有一个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", "");
}
Run Code Online (Sandbox Code Playgroud)
我想SharedPreferences在我的应用程序卸载时删除它.
Mah*_*raa 276
问题不在于偏好.这是备用经理!..因为android-23默认备份作为任务存储应用程序的数据,包括首选项到云.稍后当您卸载然后安装较新版本时,您可能会使用恢复的首选项.为避免这种情况,只需将其添加到清单(或至少调试清单):
<application ...
android:allowBackup="false">
...
</application>
Run Code Online (Sandbox Code Playgroud)
请阅读:http://developer.android.com/guide/topics/data/backup.html
如果你在下面运行Lint,你也会看到Android > Lint > Security:
这里提一下,备份过程就像一个黑盒子......你不知道它什么时候开始,以及检查之间的时间......所以最好开发禁用它.
或者,您可以在卸载应用程序之前清除缓存.
我希望这可能有所帮助.
Dha*_*mar 18
SharedPreferences 始终与应用程序卸载一起删除.
卸载任何应用程序时,应用程序在内部存储器中所做的所有更改都将被撤消,这意味着您的SharedPreference文件,其他数据文件,数据库文件,应用程序将被Android操作系统自动删除.
编辑:2015年4月29日:对于> = 21 API,请参阅@Maher Abuthraa的回答
Hir*_*tel 10
它很奇怪,但我通过以下方式找到了解决方案:
xmlns:tools="http://schemas.android.com/tools"在清单标签中的Manifest.xml文件android:allowBackup="false"在应用程序代码中的Manifest.xml文件tools:replace="android:allowBackup"在应用程序代码中的Manifest.xml文件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>
Run Code Online (Sandbox Code Playgroud)
完成.
小智 7
问题不在于偏好。
使用此代码修复它.......
<application
android:allowBackup="true"
android:fullBackupContent="false"></application>
Run Code Online (Sandbox Code Playgroud)
设置allowBackup="false"选择应用程序退出备份和恢复。
android:allowBackup="false"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40084 次 |
| 最近记录: |