自从6.0以后部分权限需要用户手动同意才可获取
即危险权限:
- CALENDAR
- CAMERA
- CONTACTS
- LOCATION
- MICROPHONE
- PHONE
- SENSORS
- SMS
- STORAGE
第一步:
//判断系统的SDK版本,23是对应6.0
if (Build.VERSION.SDK_INT>=23) {
//判断是否拥有某个权限,这里举例为Camera权限
if (!(checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
//未拥有该权限执行操作
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA_CODE);
}
else{
//已经拥有该该权限执行的操作
}
}
else{
//系统版本在小于23,即6.0以下,AndroidManifest下申请权限,执行操作
}
第二步:
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_PERMISSION_CAMERA_CODE) { int grantResult = grantResults[0]; boolean granted = grantResult == PackageManager.PERMISSION_GRANTED; if (granted){//同意之后执行的操作 } } }