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

时间:2013-04-08 06:59:00

标签: 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", "");   
   } 

我想在卸载应用程序时删除此SharedPreferences

6 个答案:

答案 0 :(得分:249)

问题不在于偏好。它非常备份管理器! ..因为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"

在application元素中。 感谢@ shahzain-ali

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

我希望这可能会有所帮助。

答案 1 :(得分:18)

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

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

编辑:29/04/15:for&gt; = 21 API参考@Maher Abuthraa 's answer

答案 2 :(得分:8)

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

  1. Manifest.xml 文件的清单标记中添加xmlns:tools="http://schemas.android.com/tools"
  2. Manifest.xml 文件的应用程序代码中添加android:allowBackup="false"
  3. Manifest.xml 文件的应用程序代码中添加tools:replace="android:allowBackup"
  4. 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 :(得分:3)

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

 android:allowBackup="false"

答案 4 :(得分:2)

问题不在于偏好。

使用此代码进行修复..........

<application
    android:allowBackup="true"
    android:fullBackupContent="false"></application>

答案 5 :(得分:0)

从棉花糖开始,共享首选项不再总是被删除。在清单中添加以下行:“ android:allowBackup =” false“”