Android install apk programatically

2019-08-20 09:11发布

问题:

I was having some problem when trying to install the apk programatically 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. Any ideas?

Thanks in advanced!

回答1:

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!