简体   繁体   中英

Android - Install APK via intent - react to failure

I am installing an APK via the following intent:

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. 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?

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.

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.

These options won't be perfect and might not work on all implementations but might lead you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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