需要安装由下载管理器下载的apk。安装意味着,显示安装页面。我想在Android 24 API,Nougat中实现。我在res的xml文件夹中创建了provider_path命名文件。并在Android清单中添加了提供程序。每当我尝试传递URI时,它都会显示安装程序崩溃或apk不正常。请帮忙。
我的代码: 提供者路径xml provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
Android Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$$$$$.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
运行安装程序弹出:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = null;
if (apkPath.substring(0, 7).matches("file://")) {
apkPath = apkPath.substring(7);
}
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", new File(apkPath));
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}