昨天做练习,在低版本下屡试不爽的电话权限申请失效了,原来就是在AndroidManifests.xml 中添加
<uses-permission android:name="android.permission.CALL_PHONE"/>
就好了, startActivity(intent); 的时候也不会报错;
但是昨天在写 startActivity(intent); 的时候as就提醒要检查权限,自动补全后也不能打电话,
google发现,新版本需要在执行特殊权限要求的动作钱进行权限检查.没有获得权限的话是不能执行的,
后来解决如下:
private void collPhone(String phone) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {//检查是否获得了通话权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0);//没有获得就申请获取
} else {//否则通话 這里的权限申请是新版本特性
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phone));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}