Android“记住"了完成应用卸载后的共享首选项 [英] Android "remembers" shared preferences after complete app uninstall

查看:76
本文介绍了Android“记住"了完成应用卸载后的共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我使用一组随机生成的文件名来存储一些数据.为了确保在应用程序重新启动时生成相同的文件名集-并且对于每个应用程序安装它们都不同-我通过从长串随机字母数字字符中选择名称作为子字符串来开始该过程,该字符串由我在应用已安装.后一个字符串存储在共享首选项"中.

In my Android app I use a set of randomly generated file names to store some data. In order to ensure that the same set of file names are generated at app restart - and are yet different for each app installation - I start off the process by picking the names as substrings from a long string of random alphanumeric characters which I generate when the app is installed. This latter string is stored in Shared Prefereneces.

在测试该应用程序时,遇到了一个非常奇怪的问题.我会不时地进行重大更改,因此我会完全卸载该应用程序-甚至强制关闭该应用程序+清除其所有数据.到那时,如果重新安装该设备,我希望该设备不具备该应用程序的先验知识".但是我发现共享首选项"字符串以某种方式被记住".如果在此期间我更改了从存储的共享首选项字符串中提取文件名子字符串的方式,则会造成严重破坏.

As I am testing out the app I have run into a rather peculiar issue. From time-to-time I make major changes so I fully uninstall the app - and even Force Close it + clear all its data. At that point I would expect that the device would have no "prior knowledge" of the app if it is reinstalled. And yet I find that the Shared Preferences string is somehow "remembered". This causes havoc if in the interim I have changed how the file name substrings are picked up from the stored shared preferences string.

如何确保应用程序具有先前安装版本的零内存",并随后将其卸载?

How can I ensure that the app has "zero memory" of a previously installed version that has subsequently been uninstalled?

我过去使用的一种解决方案是指示Android不要通过清单文件android:allowBackup = "false"进行任何备份.但是,自从我放弃了这个想法,因为-除非我弄错了-这实际上意味着我在阻止用户决定更换手机时阻止用户将其应用程序移植到新设备上.

One solution I have used in the past is to instruct Android not to do any backups via the manifest file, android:allowBackup = "false". However, I have backed away from that idea since - unless I am mistaken - it effectively means that I am stopping the user from porting their app over to a new device when they decide to change handsets.

推荐答案

在(重新)安装时,您的应用程序可能正在通过Google自动备份(通过Google云端硬盘)还原文件.要禁用此功能,可以在清单中将其显式设置为false:

On (re)install, your app may be restoring files from Google auto-backup (via Google Drive). To disable this feature, you can explicitly set it to false in the manifest:

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

如果您希望对备份/还原的内容和非备份/还原的内容进行更精细的控制,则可以在备份中包括或排除特定文件.

If you'd like more granular control over what is backed up/restored and what is not, you can include or exclude specific files from the backups.

请参阅自动备份文档: https://developer.android.com/guide/topics/data/autobackup#启用自动备份

See auto backup documentation: https://developer.android.com/guide/topics/data/autobackup#EnablingAutoBackup

如果您不想禁用自动备份,但想使用干净的平板"重新安装(出于测试目的),则可以执行以下操作之一:

If you don't want to disable auto backups, but want to reinstall with a "clean slate" (for testing purposes), you can do one of the following:

  • 重新安装后 清除应用数据.这将清除安装时自动恢复的文件
  • 清除应用数据之前以将其卸载,然后使用以下命令强制执行新备份(这样旧备份将被重置):adb shell bmgr backupnow <PACKAGE>
  • Clear app data after reinstall. This will wipe out files that were restored automatically on install
  • Clear app data prior to uninstall, then force a new backup (so old one gets reset) by using this command: adb shell bmgr backupnow <PACKAGE>

查看如何测试备份文档: https://developer.android.com/guide/topics/data/testingbackup# TestingBackup

See how to test backups documentation: https://developer.android.com/guide/topics/data/testingbackup#TestingBackup

这篇关于Android“记住"了完成应用卸载后的共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆