有什么方法可以创建 "permanent" 文件吗?

Is there any way to create a "permanent" file?

"permanent" 我的意思是它拒绝 Android OS 提供的应用程序卸载选项。显然你不能让一个文件在用户的终端中不可删除,至少,如果用户愿意,他总是可以通过文件管理器删除它。

我需要这个,因为在我的应用程序中,当执行了某些操作后,应用程序禁止执行更多操作。到目前为止,这是通过一个文件来控制的,但没有什么可以阻止用户卸载应用程序,并且在全新安装后不会再发生这种情况。

我可以实施某种类型的服务器端逻辑来阻止用户继续,但是:

1)通过检查文件更容易预防

2) 如果用户设法绕过这个安全措施会发生什么并不重要,所以我真的不介意他们中的一些人是否能够绕过保护,只要文件可以"permanent" 并且在一些晦涩的目录中,没有多少用户能够执行上述行为。

有什么办法吗?

而不是 file ,我建议使用以下意图来捕捉卸载您的应用程序并使用您的逻辑来允许或拒绝卸载

ACTION_PACKAGE_REMOVED -:广播操作:现有应用程序包已从设备中删除。数据包含包的名称。正在安装的包未收到此 Intent。

ACTION_PACKAGE_FULLY_REMOVED - :广播操作:现有应用程序包已从设备中完全删除。数据包含包的名称。这类似于 ACTION_PACKAGE_REMOVED,但仅当 EXTRA_DATA_REMOVED 为真且 EXTRA_REPLACING 为假时才设置该广播。

这可以通过配置 auto-backup 来完成,假设用户已启用它。

例如,我已经解释了如何在调试模式下禁用该行为。

先进之处在于,它适用于多个设备,绑定到帐户。

只要获得写文件权限,将文件写入根文件目录即可。如此处所述:https://developer.android.com/training/data-storage/files#WriteExternalStorage

After you request storage permissions and verify that storage is available, you can save two different types of files:

  • Public files: Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user. For example, photos captured by your app or other downloaded files should be saved as public files.
  • Private files: Files that rightfully belong to your app and will be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they don't provide value to the user outside of your app.