Android以编程方式安装apk? [英] Android install apk programmatically?

查看:32
本文介绍了Android以编程方式安装apk?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试以编程方式安装 apk 并在安装时重新启动 Android 模拟器时遇到了一些问题.我提到了这个话题.

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

这是我的代码:

    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);

有没有办法在不启动意图的情况下安装apk?因为我在 AsyncTask 的 doInBackground() 中执行上面的方法.然后在 onPostExecute() 中,我需要显示一个说明安装成功的片段.

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.

但是,使用上面的代码,在调用 startActivity() 时,它只是关闭了我的所有片段.

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

推荐答案

不幸的是,您无法完全在后台安装应用程序(假设这是您尝试通过使用 doInBackground()) 无需用户干预.当您启动该意图时,您只需将意图传递给系统的包管理器并要求它安装它.包管理器将需要要求用户确认.没有 root 或特殊权限,即使有 android.permission.INSTALL_PACKAGES 权限,您也无法真正在后台以编程方式安装 APK.希望这能回答您的问题!

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!

这篇关于Android以编程方式安装apk?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆