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

查看:92
本文介绍了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()启动Intent来尝试这样做).用户干预.启动该Intent时,您只是将Intent传递给系统的程序包管理器,并要求其安装它.包裹管理者将需要询问用户确认.没有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天全站免登陆