android 8.0安装apk失败

本文详细介绍了在Android8.0及以上版本中安装APK的步骤,包括配置FileProvider,添加必要的权限,以及根据不同API级别调整Intent的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 8.0安装apk除了要配置provider

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

在res目录下添加 xml目录 创建file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--外部存储路径-->
    <external-path
        name="files_root"
        path="Android/data/包名/" />
    <external-path
        name="external_storage_root"
        path="." />
</paths>

还要添加这个权限

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

调用方式

Intent i = new Intent();
i.setAction("android.intent.action.VIEW");
i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判读版本是否在7.0以上
    Uri apkUri = FileProvider.getUriForFile(MainApplication.appContext, MainApplication.appContext.getPackageName()
                                    + ".fileprovider", new File(filePath));
    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    i.setDataAndType(apkUri, "application/vnd.android.package-archive");
    
 } else {
      i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
}
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);MainApplication.appContext.startActivity(i);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值