android8.0应用内app安装

本文介绍了如何在Android 8.0上进行应用内安装的适配,包括创建file_paths.xml文件,添加必要的权限如下载、网络和安装来源权限,配置fileprovider以及调用安装程序的步骤。

由于android7.0应用内共享文件以及8.0需要应用安装未知来源的权限,所以需要适配7.0以及8.0;

1.首先在res文件夹里创建xml文件夹,然后添加file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
	<paths xmlns:android="http://schemas.android.com/apk/res/android">
    	<root-path name="root" path="" />
    	<external-path name="external_storage_root" path="." />
    	<external-path name="external_storage_download" path="Download" />
	</paths>

2.manifest文件需要添加添加下载权限,网络权限以及其他权限,最重要的是要添加安装位置来源的权限:

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

3.给manifest添加fileprovider

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="你的包名.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

4.调用安装程序

public static void installApp(Context pContext, File pFile){
        if (null == pFile)
            return;
        if (!pFile.exists())
            return;
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            uri = FileProvider.getUriForFile(pContext, pContext.getPackageName() + ".fileProvider", pFile);
        }else {
            uri = Uri.fromFile(pFile);
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        pContext.startActivity(intent);
    }

到这里也就大功告成了;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值