当前位置 : 主页 > 手机开发 > android >

详解Android 检测权限的三种写法

来源:互联网 收集:自由互联 发布时间:2021-05-17
本文介绍了详解Android 检测权限的三种写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 权限检测生效条件: targetSdkVersion 以及 compileSdkVersion

本文介绍了详解Android 检测权限的三种写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

权限检测生效条件:

  1. targetSdkVersion 以及 compileSdkVersion 升级到 23 及以上
  2. 运行 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;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论