Android7.0以上系统无法跳转到APK安装页

先表明原文地址,这几行代码都一样,随便找了一篇https://www.jianshu.com/p/9fdd84274954

但是!我的问题是按照上述文章处理后,不报FileUriExposedException了  ,但依然不跳转安装页面,也不报错,也不跳转,毫无头绪,很头疼,不知道问题在哪,所以这是最恶心的地方。

我是加上了一个安装权限就好了

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

因为7.0以上的手机都会有一个“未知来源应用权限”的开关,默认是不允许的,加上这个之后,就会自动跳转到这个开关页面,打开就好了,不打开还是不会跳到安装页面

好了,下面说一下通用的步骤:

1, 定义FileProvider

<manifest>
    ...
    <application>
        ...
   
         <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.test.CrossBorderSellPlatform.fileprovider"//自定义名字 为避免重复建议设为:包名.fileprovider
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
        ...
    </application>
</manifest>

 

2,指定可用的文件路径  (我是这么写的   试过别人的  但是对我好像不太好使)

在项目的res目录下,创建xml文件夹,并新建一个provider_paths.xml文件。通过这个文件来指定文件路径。

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

3, 开始跳转

/**
     * @param file
     * @return
     * @Description 安装apk
     */
    protected void installApk(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+以上版本
            Uri apkUri = FileProvider.getUriForFile(context, "com.test.CrossBorderSellPlatform.fileprovider", file);  //包名.fileprovider
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

4.如果这三步完成后,你那里跟我一样不好使,就在清单文件把最上面那个权限加上

 

纠结了我两天,网上找的办法都是前三步那个样子,总算是完事了,今晚不用加班了,鼓掌!!!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值