一聚教程网:一个值得你收藏的教程网站

热门教程

Android 8.0安装apk的实例代码

时间:2022-06-25 23:02:23 编辑:袖梨 来源:一聚教程网

android 8.0安装apk需要请求未知来源权限

//xml配置

/**
 * 检测版本8.0
 */
public void checkOreo() {
  if (SdkVersionUtils.hasOreo()) {//8.0
    //判断是否可以直接安装
    boolean canInstall = getPackageManager().canRequestPackageInstalls();
    if (canInstall) {
      //rxpermissions请求权限
      mRxPermissions
          .request(Manifest.permission.REQUEST_INSTALL_PACKAGES)
          .subscribe(granted -> {
            if (granted) {
              //安装apk
              installApk();
            } else if (shouldShowRequestPermissionRationale(Manifest.permission.REQUEST_INSTALL_PACKAGES)) {
               //引导用户去打开权限
              Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
              startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
            } else {
              Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
              startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
            }
          });
    } else {
      //安装apk
      installApk();
    }
  } else {
    //安装apk
    installApk()
  }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch (requestCode) {
    case GET_UNKNOWN_APP_SOURCES:
      checkOreo();
      break;
  }
}

热门栏目