2

I update my app using a local downloaded .apk, via DownloadManager, using this code:

val installIntent = Intent(Intent.ACTION_VIEW)
            installIntent.setDataAndType(localUri, "application/vnd.android.package-archive")
            installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            this.startActivity(installIntent) 

Although most of the time it works, sometimes, only some of the users get the "There was a problem parsing the package error" popup.

I'm interested if there is any callback that we can get in our app, whenever this happens, or user press 'OK' in the popoup . I found here for instance, a callback that checks if the package exists (meaning that it was installed) but I'm more interested for updates, in this case I already know that the app was previously installed.

Is there a callback (maybe startActivityForResults) for this specific intent, so we know that it was successfully installed or it failed?

AlexAndro
  • 1,918
  • 2
  • 27
  • 53
  • Perhaps, rather than use `ACTION_VIEW`, you could use [`PackageInstaller`](https://developer.android.com/reference/kotlin/android/content/pm/PackageInstaller). [Here is a sample project](https://gitlab.com/commonsguy/cw-android-q/-/tree/vFINAL/AppInstaller) demonstrating its use. – CommonsWare Oct 29 '21 at 10:59
  • @CommonsWare It worked using package installer. At least for unsuccessfully installation, for success usually goes to launcher main screen. If you turn your comment into a response, I'm gonna accept it. Thanks. – AlexAndro Nov 05 '21 at 07:25

1 Answers1

1

ACTION_VIEW is not designed to return a result. On Android 5.0 and higher, PackageInstaller gives you a direct API for installing apps, where you can provide a callback (in the form of an IntentSender) to find out about how the installation proceeds.

This sample project shows the basics of using PackageInstaller, and you should be able to find open source apps that use it in a more sophisticated fashion. For example, the F-Droid client probably uses PackageInstaller.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491