0

I was having some problem when trying to install the apk programmatically and reboot the Android emulator upon installation. I referred to this thread.

Here is my code:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
    intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mActivity.startActivity(intent);

Is there any way to install the apk without starting an intent? Because I am executing the method above in doInBackground() of my AsyncTask. Then in onPostExecute(), I need to show a fragment stating that the installation is successful.

However, with the code above, upon calling the startActivity() it just closed all my fragments.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Here you can find what yu are looking for: [INSTALL APK PROGRAMMATICALLY](https://stackoverflow.com/questions/59105199/how-to-install-any-android-app-programmatically-in-android-10/63708165#63708165) – kinghomer Feb 12 '21 at 11:17

1 Answers1

0

Unfortunately, you can't install an app completely in the background (assuming that's what you're trying to do by launching the Intent with doInBackground()) without user intervention. When you launch that intent, you're simply passing off the Intent to the system's package manager and asking it to install it. The package manager will need to ask the user for confirmation. Without root or special privileges, there's no way for you to truly install an APK programmatically in the background even with the android.permission.INSTALL_PACKAGES permission. Hope this answers your question!

Brian
  • 7,955
  • 16
  • 66
  • 107
  • Thanks so much! But can I check with you on how the installation works? I am not really quite sure about that. Basically when I call the method above, it just grab the apk from the path I specified and perform the installation? After that, it closes all my fragment and show my Activity that opened up those fragment instead. Then how can I check if it is successfully installed? –  Jun 08 '18 at 00:52
  • Because right after it opens up the new intent, I tried to use the same code to retrieve version number, it is still showing the one without upgrade –  Jun 08 '18 at 00:57
  • When you launch the Intent, the system does not immediately start installing the app. It will actually ask the user whether they want to confirm installation or not. To determine if your package was successfully updated/installed you can listen for a system broadcast via a BroadcastReceiver. See: https://stackoverflow.com/a/16576305/394933 – Brian Jun 08 '18 at 00:58
  • But I did not get any prompt upon restarting the intent. Do you have any ideas? Also, how do I call the onReceive() from the hyperlink you provided? –  Jun 08 '18 at 01:00
  • Are you still trying to launch the Intent from `doInBackground()`? If so, don't do that, you should run `startActivity(Intent)` from the main thread (just normally, nothing special). If you haven't worked with BroadcastReceivers before, check out: https://developer.android.com/guide/components/broadcasts – jump to the "Receiving broadcasts" section. Not enough room to explain here in the comments, but essentially you need to declare the IntentFilter you want to listen to and then register the receiver. – Brian Jun 08 '18 at 01:06
  • But then before launching the intent, I have an AsyncTask to grab the latest apk. So in this case, can I launch the intent from onPreExecute or onPostExecute? Because if I put it in main thread, it does not wait until the AsyncTask finish before executing –  Jun 08 '18 at 01:38
  • Hey do you have any ideas? –  Jun 08 '18 at 06:08
  • This is starting to go a bit beyond your original question, but I from what it sounds like, I would say you'd probably want to run it in the `onPostExecute` of your AsyncTask. Be careful with AsyncTasks though, they are easily prone to leaking memory if not handled correctly. – Brian Jun 08 '18 at 06:31
  • Yeap I tried it already. It does not work still. I received no prompt. All it does is just restart the intent n that's it. –  Jun 08 '18 at 06:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172731/discussion-between-brian-and-guest176969). – Brian Jun 08 '18 at 06:37