如何在android中删除应用程序时删除共享首选项

问题描述 投票:86回答:5

我有一个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 android-preferences
5个回答
258
投票

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

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

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

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

lint warning on backup

这里提一下,备份过程就像一个黑盒子......你不知道它什么时候开始,以及检查之间的时间......所以最好开发禁用它。

====更新====

在将allowbackup设置为false之后,您可能会收到Manifest合并问题。要解决该问题,请添加:

tools:replace="android:allowBackup"

在应用程序元素中。感谢@ shahzain-ali

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

我希望这可能有所帮助。


18
投票

SharedPreferences始终与应用程序卸载一起删除。

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

编辑:29/04/15:对于= = 21 API,请参阅@Maher Abuthraa 's answer


8
投票

它很奇怪,但我通过以下方式找到了解决方案:

  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>

完成。


3
投票

设置allowBackup="false"会选择备份和还原中的应用程序。

 android:allowBackup="false"

2
投票

问题不在于偏好。

使用此代码修复它..........

<application
    android:allowBackup="true"
    android:fullBackupContent="false"></application>
© www.soinside.com 2019 - 2024. All rights reserved.