申请权限:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//如果 API level 是大于等于 23(Android 6.0) 时
//判断是否具有权限
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PERMISSION_GRANTED) {
//如果应用之前请求过此权限但用户拒绝了请求,此方法将返回 true。
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.权限)) {
}
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.权限},
0);
}
}
申请结果:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PERMISSION_GRANTED) {
//申请成功(用户点击允许)
} else {
//申请失败(用户点击拒绝)
}
}
}
}