Android - 保留或删除应用程序卸载时创建的文件
我创建了一个应用程序,用于创建文件并将其存储到 SD 卡。 有没有办法将文件夹与应用程序绑定,以便当用户在 Android 设备上运行卸载时删除所有文件?
I created an application which creates and stores files to sdcard. Is there a way to bind the folder with application in order to delete all files when the user runs uninstall on android device?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
自 2009 年以来似乎有了一些进展:)。
来自文档:
Seems like there have been some developments since 2009 :).
From the documentation:
我添加此内容是因为现有答案在 2017 年已过时。
虽然应用程序卸载确实没有回调,但已经提供了一些机制来保留卸载后的应用程序数据。
自 Android 6.0 (API 23) 起,自动备份
应用
引入了该功能,该功能允许开发人员允许或阻止自动应用程序备份。 自 API 23 起默认启用备份。
清单文件引入了两个新的
application
标签:android:allowBackup
和android:fullBackupContent
。要选择退出自动备份,请将
android:allowBackup="false"
添加到application
标记下的清单文件中。 值为“true”将自动将共享首选项和数据库保存到云端,以及 其他文件。其次,您可以非常具体地指定要在卸载时包含或排除删除或保留的文件使用
android:fullBackupContent
标签。 该属性指向包含备份规则的 XML 文件。 使用以下语法在res/xml/
目录中创建 XML 配置文件:您可以在此处添加要包含或排除的特定文件类型及其关联路径。 将
android:fullBackupContent = "@xml/my_backup_rules"
标记添加到application
标记下的清单文件中。指定的文件将备份到设备主电子邮件帐户下的 Google 云端硬盘。 只要用户启用以下设置,重新安装时数据就会恢复:
设置 -> 备份与备份 重置-> 自动恢复。
还应该注意的是,用户或设备上的其他应用程序无法读取备份数据。
也可以从此处的 Google 云端硬盘应用程序访问该内容:
Google 云端硬盘 -> 设置-> 应用程序自动备份 -> 备份和重置
值得注意的是,如果多个设备使用同一个主 Google 帐户,则会创建多个设备备份。 可以从此处访问各个设备的备份:
Google Drive -> 设置-> 备份
I'm adding this because the existing answers are outdated in 2017.
While it is true that there is no callback on app uninstall, some mechanisms have been provided for preserving app data beyond uninstall.
Since Android 6.0 (API 23), the Auto Backup for
Apps
feature was introduced, which allowed developers to either permit or prevent automatic application backup. As of API 23 backup is enabled by default.
Two new
application
tags have been introduced for the manifest file:android:allowBackup
andandroid:fullBackupContent
.To opt out of automatic backup, add
android:allowBackup="false"
to the manifest file under theapplication
tag. A value of "true" will automatically save shared preferences and databases to the cloud, as well as other files.Secondly, you can be very specific about the files you want to include or exclude for deletion or preservation on uninstall with the
android:fullBackupContent
tag. This attribute points to an XML file that contains backup rules. Create an XML configuration file in theres/xml/
directory with the following syntax:Here you can add the specific file types to include or exclude and their associated paths. Add the
android:fullBackupContent = "@xml/my_backup_rules"
tag to the manifest file under theapplication
tag.The specified files would be backed up to Google Drive under the primary email account of the device. The data would be restored on re-install provided the following setting is enabled by the user:
Settings -> Backup & Reset -> Automatic Restore
.It should also be noted that the backup data cannot be read by the user or other applications on the device.
The same can also be accessed from the Google Drive app from here:
Google Drive -> Settings -> Auto backup for apps -> Backup and reset
It is worth noting that if the same primary Google account is used for multiple devices, then multiple device backups are created. The backups for individual devices can be accessed from here:
Google Drive -> Settings -> Backups
您的应用程序无法知道它正在被卸载(不修改内核)。 应用程序卸载后,
data/data/your.app.package
中创建的所有文件都会自动删除。我认为 SD 卡上没有清除任何内容。 您可以进行快速测试并找出答案。
另一种方法可能是使用另一个应用程序来检查该应用程序是否已安装。 如果没有,它可以完成清理工作。
There's no way for your application to know that it is being uninstalled (without modifying the kernel). All files created in the
data/data/your.app.package
is deleted automatically upon application uninstall.I don't think anything is cleared from the sdcard. You can do a quick test and find that out.
Another approach could be to have another application that checks whether this application is installed or not. If not, it can do the clean-up work.
即使在使用 getExternalCacheDir() 时,我也发现缓存目录(即应用程序的包名称)不会像 Android 文档中所述自动删除,至少在 Lenovo A1 上不会。 这可能有点天真,但您可能想在重新安装期间清理以前的数据。 在那里,您可以了解您的应用程序是否已卸载或首次安装 - 您使用共享首选项存储一个值,可能是一个布尔值。 它可以是这样的:
这样,每次应用程序运行时,它都会检查是否是第一次运行。 当应用程序被删除时,应用程序的共享首选项也将被删除。 然后,下次安装时,if(...) 将为 true,您可以在那里进行一些清理。
我知道这并不能直接回答问题,而且在用户设备上保留未使用的数据并不好,但我认为这可能是在删除应用程序时尝试清理的另一种选择,这是明智的,但似乎不可能。
Even when using the
getExternalCacheDir()
, I have seen that the cache directory, which is your app's package name, is not removed automatically as it says on the Android Documentation, at least not on the Lenovo A1. It could be a bit naive but you may want to clean up previous data during re-install. There, you can find out if your app was un-installed or is being installed for the first time - you store a value, perhaps a boolean, using shared preferences. It can be something like:This way, everytime the app runs, it checks if it's the first time it's running. When the app is removed, so will the shared preferences for the app. Then, the next time it is installed, the if(...) will be true and you can do some clean up there.
I know this doesn't answer the question directly and it's not nice to leave unused data on users' devices, but I think it could be an alternative to trying to clean up when an app is being removed, which is sensible but seems impossible.
似乎从那时起就有了一个合适的解决方案, Context.getExternalFilesDir():
it seems since then there is a proper soltuion, Context.getExternalFilesDir():
如果您想存储大数据,请将其存储在外部存储中,并使用文件路径名称,例如
其中包名称是您的应用程序包名称,例如 com.think.abc
if you want to store large data then store it in external storage with file path name such as
where package name is your app package name eg com.think.abc