Android 动态申请 REQUEST_INSTALL_PACKAGES 权限问题:申请权限失败

xml 复制代码
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
java 复制代码
private static final int REQUEST_CODE_INSTALL_PERMISSION = 1;
java 复制代码
ActivityResultLauncher<String> activityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.RequestPermission(),
        isGranted -> {
            if (!isGranted) {
                Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }

            next();
        }
);

activityResultLauncher.launch(Manifest.permission.REQUEST_INSTALL_PACKAGES);
  • 在 Android 开发中,执行上述权限申请的代码,权限申请失败
问题描述
  1. Manifest.permission.REQUEST_INSTALL_PACKAGES 是一个特殊权限,不能通过常规的运行时权限请求方式(ActivityResultContracts.RequestPermission)来申请

  2. Manifest.permission.REQUEST_INSTALL_PACKAGES 必须通过跳转到系统设置页面让用户手动授权

  3. 正确的申请方式:检查是否已有权限,如果没有权限,引导用户前往系统设置

  4. Manifest.permission.REQUEST_INSTALL_PACKAGES 在 Android 8.0 引入,低版本无需处理

处理策略
  1. AndroidManifest.xml 中声明权限
xml 复制代码
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
  1. 跳转请求权限(过时写法)
java 复制代码
private static final int REQUEST_CODE_INSTALL_PERMISSION = 1;
java 复制代码
boolean canInstall = checkInstallPermission();

Log.i(TAG, "canInstall = " + canInstall);

if (!canInstall) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
            .setData(Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, REQUEST_CODE_INSTALL_PERMISSION);
    return;
}

next();
java 复制代码
private boolean checkInstallPermission() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? getPackageManager().canRequestPackageInstalls() : true;
}
java 复制代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE_INSTALL_PERMISSION) {
        boolean canInstall = checkInstallPermission();

        Log.i(TAG, "now, canInstall = " + canInstall);

        if (!canInstall) {
            Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();
            return;
        }
        next();
    }
}
  • 使用 ActivityResultLauncher 来替代(现代写法)
java 复制代码
private boolean checkInstallPermission() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? getPackageManager().canRequestPackageInstalls() : true;
}
java 复制代码
boolean canInstall = checkInstallPermission();

Log.i(TAG, "canInstall = " + canInstall);

if (!canInstall) {
    ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                boolean canInstall_ = checkInstallPermission();

                Log.i(TAG, "now, canInstall = " + canInstall_);

                if (!canInstall_) {
                    Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                next();
            }
    );

    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
            .setData(Uri.parse("package:" + getPackageName()));

    activityResultLauncher.launch(intent);

    return;
}
next();
相关推荐
黄林晴9 分钟前
Android17 为什么重写 MessageQueue
android
Seven9712 分钟前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
皮皮林55110 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河10 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程13 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅14 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者15 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺15 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart17 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot