问题描述
我尝试以编程方式安装APK并在安装后重新启动Android模拟器时遇到了一些问题. 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而不开始意图?因为我在我的异步箱的doInBackground()中执行了上述方法.然后,在onPostExecute()中,我需要显示一个片段,说明安装成功.
然而,使用上面的代码,调用startActivity()它只是关闭了我所有的片段.
推荐答案
不幸的是,您不能在没有用户干预的情况下完全安装一个应用程序(假设您通过使用doInBackground()启动意图来实现意图).当您启动此意图时,您只是将目的传递给系统的软件包管理器并要求它安装.软件包管理器将需要向用户询问确认.没有根或特殊特权,即使有android.permission.INSTALL_PACKAGES权限,您也无法在背景中真正安装APK.希望这回答您的问题!
问题描述
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.
推荐答案
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!