时间:2020-10-08 13:27:55 | 栏目:Android代码 | 点击:次
一款不错的android6.0、7.0权限管理器PermissionsCheckerUtil
初始化权限管理器:构造方法
private final Context mContext; public PermissionsCheckerUtil(Context mContext) { this.mContext = mContext; }
判断使用的权限集合是否已经全部允许
// 判断权限集合 public boolean lacksPermissions(String... permissions) { for (String permission : permissions) { if (lacksPermission(permission)) { return true; } } return false; }
判断是否缺少权限的核心方法
// 判断是否缺少权限 private boolean lacksPermission(String permission) { return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED; }
使用:
//添加你所需权限进集合 private static final String[] PERMISSIONS = new String[]{ Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }; public Context mContext; public static final int PERMISSION_REQUEST_CODE = 10001; private PermissionsCheckerUtil mPermissionsCheckerUtil; //权限检测器
在oncreate中初始化权限管理器
mContext = getApplicationContext(); mPermissionsCheckerUtil = new PermissionsCheckerUtil(mContext); initPermission();
初始化判断方法initPermission
private void initPermission() { if (mPermissionsCheckerUtil.lacksPermissions(PERMISSIONS)) { ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_REQUEST_CODE); } }
完成!