问题描述
我正在尝试让我的应用自动安装apk.这对于api< 24来说效果很好.但是对于24,它正在失败. Android已实现了额外的安全性:
I am trying to get my app to automatically install an apk. This works fine for api<24. But for 24, it is failing. Android has implemented extra security:
所以我尝试了这个:
Uri myuri;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
myuri = Uri.parse("file://"+outapk);
} else {
File o = new File(outapk);
myuri = FileProvider.getUriForFile(con, con.getApplicationContext().getPackageName() + ".provider", o);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(myuri,"application/vnd.android.package-archive");
con.startActivity(promptInstall);
但遇到致命异常:
com.android.packageinstaller "Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{b42ee8a 6826:com.android.packageinstaller/u0a15} (pid=6826, uid=10015) that is not exported from uid 10066".
我的清单中有export = true.
I have export=true in my manifest.
问题似乎是packageinstaller无法使用content://uri.
The problem seems to be that packageinstaller cannot use a content:// uri.
是否有任何方法允许应用程序通过api24通过编程方式安装apk?
Are there any ways to allow an app to progammatically install an apk with api24?
推荐答案
将addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
添加到您的promptInstall
设置中,以授予对该内容的读取访问权限.
Add addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
to your promptInstall
setup, to grant read access to the content.
不在您的FileProvider
上,因为这会导致您的应用崩溃.
Not on your FileProvider
, as that would cause your app to crash.
否,问题在于您没有授予软件包安装程序读取该Uri
的权限.如果程序包安装程序无法使用content
方案,那么您将获得ActivityNotFoundException
.
No, the problem is that you did not grant permission to the package installer to read from that Uri
. Had the package installer been unable to use a content
scheme, you would have gotten an ActivityNotFoundException
.
但是请注意,只有在Android 7.0中,程序包安装程序才开始支持content
.早期版本的Android必须使用file
.
Note, though, that it is only with Android 7.0 that the package installer starts supporting content
. Earlier versions of Android have to use file
.
这篇关于在Android 7/API24中以编程方式安装apk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!