2

I would like to implement an on click event which prompts the user to download an APK to update my app to the latest version:

public void f(View view) {
    Uri uri = Uri.parse("https://www.example.com/app-release.apk");
    startActivity(new Intent(Intent.ACTION_VIEW, uri));
}

I build the app and deploy it to my phone. I start the app and click on the button. The browser opens. In the address bar I see very briefly the URL of the APK. Then this URL disappears and the browser goes to whatever page I visited most recently before the test. The APK is not downloaded.

I have ensured that the version number of the APK to be installed exceeds the version number of the app in which I test the button click. The URL of the APK is valid, and if I explicitly visit that URL in the browser, the APK installs. I enabled android.permission.INTERNET in the manifest. On the phone I have allowed installation of apps from unknown sources, and in Play Store I turned off scanning of non playstore apps.

Any idea what I'm doing wrong?

Edit #1: I am not trying to implement code within my app to download the APK to local storage. I'm simply trying to launch a browser window pointed at the URL of the APK. I'm attempting something similar to the approach described here:

https://juristr.com/blog/2011/02/coding-update-functionality-for-your/

In that solution, the code to prompt for the download of the APK is:

Intent updateIntent = new Intent(Intent.ACTION_VIEW,
       Uri.parse("http://some-public-url/deploy/MyApplication.apk"));
startActivity(updateIntent);

That's what I want to do. But it doesn't begin the download. The URL flashes briefly in the address bar, but then the browser goes back to whatever page it was on before.

Edit #2: If I kick it off using the debugger and the virtual device, the code behaves as I expect. If I install the APK to my physical device, and click on the button there, it malfunctions as I described above. Both the physical device and the virtual one are the Nexus 5.

I have created two versions of the APK, a "version 1" and a "version 2". From within version 1 I would like to trigger a download and install of version 2.

  • If you want to download something directly, you should me looking into Android's Download Manager API. It's pretty simple, and will be more reliable. – TheWanderer Sep 11 '18 at 19:23

2 Answers2

2

I figured it out. The APK that you want to install must have the same name as the APK which spawned the instance that is attempting the download. I tried to introduce a version number into the name of the APK, that's what broke it. Many thanks for all of the kind assistance!

edit: see @kelalaka's answer, which is more complete.

1

Edit : Collected all information so that we can have a check list

  • Check that application has Permission to WRITE_EXTERNAL_STORAGE
  • Check that application as permission REQUEST_INSTALL_PACKAGES
  • Check that the device has enough space in the device
  • If your URL is http, does the browser support http?
  • The APK that you want to install must have the same name as the APK which installed the instance that launches the request.
  • any suspicious info on the Logcat?

Previous : Did you get permission to Write External?

This is similar to your problem

kelalaka
  • 5,064
  • 5
  • 27
  • 44
  • thanks for getting back to me. it seems unlikely to me that WriteExternal is relevant, it relates to downloading a file to local storage, i'm simply attempting to launch a browser window pointed to the URL of the APK. that page you linked mentions the permission REQUEST_INSTALL_PACKAGES and i tried adding that but it didn't help. – user2022499 Sep 11 '18 at 21:09
  • did 3rd part installation is turned on on phone settings? – kelalaka Sep 11 '18 at 21:29
  • On the phone I have allowed installation of apps from unknown sources, and in Play Store I turned off scanning of non playstore apps. – user2022499 Sep 11 '18 at 21:31
  • enough space on the device? – kelalaka Sep 11 '18 at 21:55
  • i think there must be, because if i go in to the browser and visit the URL of the APK, it prompts me to download it, then installs it successfully. i just want to trigger that behavior from within my app. – user2022499 Sep 11 '18 at 21:57
  • Anything unusual log cat? – kelalaka Sep 11 '18 at 22:03
  • i just kicked it off using the debugger and the virtual device, so that i could see the log file - and in that environment the code behaves as i expect. if i install the APK to my physical device, and click on the button there, it fails as i described in the OP. both the physical device and the virtual one are the nexus 5. – user2022499 Sep 11 '18 at 22:09
  • Your URL is http not https maybe the browser refuse this from intents? – kelalaka Sep 11 '18 at 22:16
  • What phone you have? What are the android versions? – kelalaka Sep 11 '18 at 22:18
  • i have not given you the real URL that i am using. in fact it is https. i tried experimenting with http, www, etc., no joy. i'm on the nexus 5. – user2022499 Sep 11 '18 at 22:32
  • 1
    awesome. many thanks for your edit. i accepted your answer. may i suggest that you edit "The APK that you want to install must have the same name as the APK..." and append: "... which installed the instance that launches the request." – user2022499 Sep 22 '18 at 13:47