public class PermissUtil {public static final int PERMISSON_REQUESTCODE = 123;/*** app需要进行检测的权限数组* 根据自己项目情况去添加权限*/public static String[] appNeedPermissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};/*** app权限检测**/public static boolean checkPermissions(Activity activity, String... permissions) {List<String> needRequestPermissonList = findDeniedPermissions(activity, permissions);if (null != needRequestPermissonList&& needRequestPermissonList.size() > 0) {ActivityCompat.requestPermissions(activity,needRequestPermissonList.toArray(new String[needRequestPermissonList.size()]),PERMISSON_REQUESTCODE);return false;} else return true;}/*** 获取权限集中需要申请权限的列表** @param permissions* @return*/public static List<String> findDeniedPermissions(Activity contexts, String[] permissions) {List<String> needRequestPermissonList = new ArrayList<String>();for (String perm : permissions) {if (ContextCompat.checkSelfPermission(contexts,perm) != PackageManager.PERMISSION_GRANTED) {needRequestPermissonList.add(perm);} else {if (ActivityCompat.shouldShowRequestPermissionRationale(contexts, perm)) {needRequestPermissonList.add(perm);}}}return needRequestPermissonList;}/*** 检测是否说有的权限都已经授权** @param grantResults* @return*/public static boolean verifyPermissions(int[] grantResults) {for (int result : grantResults) {if (result != PackageManager.PERMISSION_GRANTED) {return false;}}return true;}
用法:
PermissUtil.checkPermissions(this, PermissUtil.appNeedPermissions);
转载出处:https://blog.csdn.net/Jm0218Xx/article/details/107107265