Android 检测权限的三种写法

权限检测生效条件:
  • targetSdkVersion 以及 compileSdkVersion 升级到 23 及以上
  • 运行 Android 系统 6.0 及以上
三种检测权限写法:
    public static boolean checkPermission1(Context context, String[] permissions) {
        PackageManager packageManager = context.getPackageManager();
        String packageName = context.getPackageName();

        for (String permission : permissions) {
            int per = packageManager.checkPermission(permission, packageName);
            if (PackageManager.PERMISSION_DENIED == per) {
                Log.w(TAG, "required permission not granted . permission = " + permission);
                return false;
            }
        }
        return true;
    }

    public static boolean checkPermission2(Context context, String[] permissions) {

        for (String permission : permissions) {
            int per =context.checkPermission(permission, Process.myPid(),Process.myUid());
            if (PackageManager.PERMISSION_GRANTED != per) {
                Log.w(TAG, "required permission not granted . permission = " + permission);
                return false;
            }
        }
        return true;
    }

    public static boolean checkPermission3(Context context, String[] permissions) {

        for (String permission : permissions) {
            int per = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
            if (PackageManager.PERMISSION_GRANTED != per) {
                Log.w(TAG, "required permission not granted . permission = " + permission);
                return false;
            }
        }
        return true;
    }

推荐阅读:
Unity 下集成第三方原生 SDK,以极光厂商通道为例
如何修改 Android 编译 Java 源文件的 JDK 版本 原生和 Cordova 为例
谷歌将对欧洲 Android 设备制造商收取其应用服务费用

    原文作者:JoshLiPan
    原文地址: https://www.jianshu.com/p/11d99a9be7d9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞