简体   繁体   English

Android-通过意图安装APK-对失败做出反应

[英]Android - Install APK via intent - react to failure

I am installing an APK via the following intent: 我通过以下意图安装APK:

val apkUri = Uri.fromFile(apkFile)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)

I know that I can retrieve the PACKAGE_ADDED broadcast and then react in case the APK was installed. 我知道我可以检索PACKAGE_ADDED广播,然后在安装APK的情况下做出反应。 However, how can I react if the installation failed? 但是,如果安装失败怎么办? Eg if the APK was not parsed correctly (eg if its for a different architecture) or simply if the user clicks "CANCEL" on the request? 例如,是否未正确解析APK(例如,是否针对不同的体系结构),或者仅是用户在请求上单击“取消”?

I also tried startActivityForResult and the corresponding onActivityForResult functions, but that seems to be independent on the user action and is immediately triggered when the intent was fired. 我还尝试了startActivityForResult和相应的onActivityForResult函数,但这似乎独立于用户操作,并且在触发意图时立即被触发。

The answer is that there is no standard way of doing it. 答案是没有标准的方法可以做到。 If you look at the source of the package manager service you will see that there is no event broadcasted for when it is not successful. 如果查看包管理器服务源,您将看到不成功时不会广播任何事件。

You could in theory do a few things... 从理论上讲,您可以做一些事情...

  1. Check to see if the package has been installed when the activity/fragment resumes after that intent is fired. 启动该意图后,在恢复活动/片段时,请检查是否已安装该软件包。
  2. Inspect the logcat logs if you have the permissions to do so and parse them for any package manger output. 检查logcat日志(如果您具有这样做的权限),并解析它们以获取任何程序包管理器输出。

These options won't be perfect and might not work on all implementations but might lead you in the right direction. 这些选项不是完美的,可能无法在所有实现上都起作用,但可能会引导您朝正确的方向发展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM