在6.0 系统中请求某些权限需要检查权限

下面以拨打电话为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (!TextUtils.isEmpty(phone)) {
if (!hasPermission()) {
// 在6.0 系统中请求某些权限需要检查权限
int curApiVersion = Build.VERSION.SDK_INT;
if (curApiVersion >= Build.VERSION_CODES.M) {
// 动态请求拨打电话权限
requestPermissions(
new String[] { Manifest.permission.CALL_PHONE }, 0x11);
} else {
intentToCall(phone);
}
} else {
intentToCall(phone);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private boolean hasPermission() {
if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return false;
}
return true;
}

private void intentToCall(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + phoneNumber);
intent.setData(data);
startActivity(intent);
}


/**
* 动态请求拨打电话权限后,监听用户的点击事件
*/
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0x11) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
CldLog.i("CMCC", "权限被允许");
String phone = mContactsInfo.getPhone();
intentToCall(phone);
} else {
CldLog.i("CMCC", "权限被拒绝");
}
}
}