Anroid 8.0 allows applications to download and install unknown source of complete logic & Adapter 7.0 file access processing

  During the update process in question 8.0 Download encountered, the download is complete install package does not update after the jump page
  that permission to install unknown applications in Android 8.0 is the switch is turned off by default, requiring the user to manually turn on the Allow
unknown source application [ authority] to be able to install. For Google Android security can be described as more and more strict.

Here's how to deal with

1, increase request permission to install in the manifest file

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

2, mainly through this method to determine whether to get permission to install

 haveInstallPermission = getPackageManager().canRequestPackageInstalls();

If haveInstallPermission true, the application has obtained permission to install applications from unknown sources, you can execute the appropriate logic install applications directly.

If haveInstallPermission false, the application did not get permission to install applications from unknown sources, you can not install the application. Since this is not permission to install the runtime permissions, so the code can no longer request permission, only playing a Dialog prompt, inform users need to install authority, confirmed the jump to set the interface to open permissions.

3, did not get permission, you need to manually go to "Settings page corresponding to the application." This allows installation settings page allows the user to manually check

    // 注意这个是8.0新API需要判断
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Uri packageURI = Uri.parse("package:" + Context.getPackageName());
            Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
            startActivityForResult(intent, REQUEST_CODE_INSTALL_PERMISSION);
        }

According to the package name to enter the settings page of this application, will not pass, then will enter a list of all the settings
(tips: value resultCode == RESULT_OK will return false if the onActivityResult returned after entering the list is set)

Complete logic:

 private static final int REQUEST_CODE_INSTALL_PERMISSION = 107;
  /**
     * 安装应用的流程  大于8.0需要用户手动打开未知来源安装权限
     * 需要在清单文件中加入权限  REQUEST_INSTALL_PACKAGES
     */
    private void installProcess() {

        boolean haveInstallPermission;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            haveInstallPermission = getPackageManager().canRequestPackageInstalls();
            if (!haveInstallPermission) {
                //没有未知来源安装权限权限
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示");
                builder.setMessage("安装应用需要打开未知来源权限,请去设置中开启应用权限,以允许安装来自此来源的应用");
                builder.setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            startInstallPermissionSettingActivity();
                        }
                    }
                });
                builder.show();
                return;//防止系统执行默认的方法会跳转页面后弹窗提示,所以会重复 return 掉
            }
        }

        installApk();
    }

   /**
     * 有权限,开始安装应用程序
     */
    private void installApk() {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
                          /*  intent.setDataAndType(Uri.fromFile(new File(getDownloadTempFileName())),
                            "application/vnd.android.package-archive");*/
        //适配Android 7.0 FileProvider文件访问,这里可能需要你修改成自己的访问方式
        FileProvider7.setIntentDataAndType(CompulsiveHelperActivity.this,
                intent, "application/vnd.android.package-archive", new File(getDownloadTempFileName()), true);
        startActivity(intent);
    }

  @RequiresApi(api = Build.VERSION_CODES.O)
    private void startInstallPermissionSettingActivity() {
        //注意这个是8.0新API
        Uri packageURI = Uri.parse("package:" + getPackageName());
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
        startActivityForResult(intent, REQUEST_CODE_INSTALL_PERMISSION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_INSTALL_PERMISSION) {
            installProcess();//再次执行安装流程,包含权限判等
        }
    }

Tips

(1), before downloading the application, you must comply with access permissions to read and write privileges to run 6.0
(2), require adaptation process 7.0 file access need to get through FileProvider. Here I use the tools of an approach HONGYANG provided FitAndroid7

About Android 7.0 behavior change through FileProvider file sharing between applications bar

Published 26 original articles · won praise 19 · views 40000 +

Guess you like

Origin blog.csdn.net/a23006239/article/details/79574031