Android 7.0(API 24)以上调用系统安装包问题

Android 7.0之后对于文件访问安全性加强,一些旧的调用方法也发生了结果异常。

Android 7.0(API 24)以前可用的安装方法

    public static boolean installApk(Context context, String apkPath) {
        File apkFile = new File(apkPath);
        if(!apkFile.exists() || !apkFile.isFile()) return false;

        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        installIntent.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive");
        installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(installIntent);

//        GNavigationBar.show(context);   ///< show navigation bar
//        android.os.Process.killProcess(android.os.Process.myPid());
        return true;
    }

会出现如下异常:

    android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.xx.xxx/cache/imrider.apk exposed beyond app through Intent.getData()

Android 7.0(API 24)以后可用的安装方法

使用自定义provider方式解决:

 public static void installMyApk(Context context, String path) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(
                    context,
                    BuildConfig.APPLICATION_ID + ".fileProvider",
                    new File(path));
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(
                    Uri.fromFile(new File(path)),
                    "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

AndroidManifest.xml中的配置:

        <!--Android 7.0(api 24)以上使用file://访问文件需要用FileProvider方式-->
        <!--file_paths为配置的可访问路径-->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.xx.xxx.fileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

资源文件中新建file_paths.xml,用于存放可以访问的文件路径定义等:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--    <external-path path="Android/data/app的包名/" name="files_root" />-->
    <external-path path="." name="external_storage_root" />
</paths>

android 8.0系统调用安装APK_xdy1120的博客-CSDN博客_android调用app安装 中说要配置一下权限:

<!--安卓8.0打开apk安装更新-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

应该是配合他自己的代码使用的:

	private void openFile(final Context context) {
		//判读版本是否在8.0以上
		if (Build.VERSION.SDK_INT >= 26) {
			//来判断应用是否有权限安装apk
			boolean installAllowed= context.getPackageManager().canRequestPackageInstalls();
			if(installAllowed){
				installApk(context);
			}else {
				installApk(context);
				new Handler(Looper.getMainLooper()).post(new Runnable() {
					@Override
					public void run() {
						ToastUtil.ToastShort(context,"请设置开启允许安装未知应用");
						//此处只做提示,系统会自动弹框提醒,并可跳转开启
//						Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + context.getPackageName()));
//						context.startActivity(intent);
					}
				});
			}
		} else {
			installApk(context);
		}
	}

注:上述部分不写应该也是没有关系的。

Android碎片化严重,系统变更有时候会带来很多意外结果。

参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luppiter.W

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值