欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-06 09:03:46
本文介绍了详解android 检测权限的三种写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 权限检测生效条件: targets...

本文介绍了详解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;
  }

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