写demo的时候,老是申请各种权限烦人,写了种根据manifest自动申请的代码
private void handlerPermissions(Activity activity) {
PackageManager pm = activity.getPackageManager();
PackageInfo pi;
List<String> allPermissionList = new ArrayList<>();
List<String> noPermissionList = new ArrayList<>();
try {
pi = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] permissions = pi.requestedPermissions;
allPermissionList = Arrays.asList(permissions);
} catch (PackageManager.NameNotFoundException e) {
Log.e("", "get permissions failed", e);
}
for (String permission : allPermissionList) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
noPermissionList.add(permission);
}
}
if (noPermissionList.size() > 0) {
ActivityCompat.requestPermissions(activity, noPermissionList.toArray(new String[noPermissionList.size()]), 1);
}
}