前言
本文是在《Android 6.0后权限的申请》基础上整理的。
本次新增了申请权限前进行检测是否有权限,没有权限的就重新申请。
好记性不如烂笔头
记录于此,方便自己查阅。
正文
public class AskPermission {
public static final int REQUEST_CODE = 0x100;
public static final String[] REQUEST_MANIFEST_PERMISSION = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
};
public static void requestPermission(Activity activity, String[] permissions, int requestCode) {
if (activity == null) {
return;
}
if (Build.VERSION.SDK_INT >= 23) {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
}
}
public static String[] checkSelfPermission(String[] permissions) {
if (null != permissions && permissions.length > 0) {
List<String> list = new ArrayList<>();
for (String value : permissions) {
if (ContextCompat.checkSelfPermission(MediaApp.getContext(), value) != PackageManager.PERMISSION_GRANTED) {
list.add(value);
}
}
if (null != list && list.size() > 0) {
return list.toArray(new String[list.size()]);
}
}
return null;
}
}
参考文章
© 版权声明