当前位置: 代码迷 >> 综合 >> 申请安装未知应用权限(android8.0以上)
  详细解决方案

申请安装未知应用权限(android8.0以上)

热度:43   发布时间:2023-12-15 15:09:24.0
    private static final int INSTALL_PERMISSION_CODE = 1;public void setInstallPermission(Context context) {boolean haveInstallPermission;// android 8.0以上安装未知应用需要开启权限if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// 先判断是否有安装未知应用权限haveInstallPermission = getPackageManager().canRequestPackageInstalls();if (!haveInstallPermission) {// 弹框提示用户手动打开new Thread(new Runnable() {@Overridepublic void run() {Looper.prepare();AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("安装权限");builder.setMessage("需要打开此应用允许安装未知应用的权限");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// 此方法需要API >= 26才能使用toInstallPermissionSettingIntent();}}});builder.setCancelable(false);AlertDialog dialog = builder.create();dialog.show();Looper.loop();}}).start();} else {Toast.makeText(MainActivity.this, "安装应用", Toast.LENGTH_LONG).show();}} else {Toast.makeText(MainActivity.this, "安装应用", Toast.LENGTH_LONG).show();}}private void toInstallPermissionSettingIntent() {Uri packageURI = Uri.parse("package:" + getPackageName());Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);startActivityForResult(intent, INSTALL_PERMISSION_CODE);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK && requestCode == INSTALL_PERMISSION_CODE) {Toast.makeText(MainActivity.this, "安装应用", Toast.LENGTH_LONG).show();}}

  相关解决方案