Android8.0手机安装apk报错解决办法

android8.0手机在进行版本更新之后安装apk闪退,这是因为android8.0需要开发人员处理未知应用来源权限问题。
解决办法:
1、在AndroidManifest中新增权限:


2、我使用的PermissionsDispatcher

/**
     * 判断是否是8.0,8.0需要处理未知应用来源权限问题,否则直接安装
     */
    private void checkIsAndroidO(File file) {
        if (Build.VERSION.SDK_INT >= 26) {
            boolean canRequestPackageInstalls = getPackageManager().canRequestPackageInstalls();
            if (canRequestPackageInstalls ) {
                openAPK(file);
            } else {
                //请求安装未知应用来源的权限
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 10010);
            }
        } else {
            openAPK(file);
        }

    }
/**
     * 打开apk
     */
    private void openAPK(File file) {
        try {
            if(Build.VERSION.SDK_INT>=24) {//判读版本是否在7.0以上
                String authority = getApplicationContext().getPackageName() + ".fileProvider";
                Uri apkUri = FileProvider.getUriForFile(SplashActivity.this,authority, file);//在AndroidManifest中的android:authorities值
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
                install.setDataAndType(apkUri, "application/vnd.android.package-archive");
                startActivity(install);
            } else{
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(install);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 10010:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    String root = Environment.getExternalStorageDirectory().getPath();
                    File file = new File(root, "你的apk.apk");
                    openAPK(file);
                } else {
                    new AlertDialog.Builder(this)
                            .setTitle("温馨提示")
                            .setMessage("请设置允许安装耀分期apk")
                            .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                                    startActivityForResult(intent, 10012);
                                }
                            })
                            .show();
                }
                break;
            case 别的:
               
                break;

            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        SplashActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10012:
                String root = Environment.getExternalStorageDirectory().getPath();
                File file = new File(root, "yaofenqi.apk");
                checkIsAndroidO(file);
                break;

            default:
                break;
        }
    }

Android7.0应用数据共享权限,FileProvider不会写的可以参考该博客[# Android 7.0 行为变更 通过FileProvider在应用间共享文件吧

你可能感兴趣的:(Android8.0手机安装apk报错解决办法)