(转)android拨打电话崩溃6.0以上实时动态权限申请

时间:2022-11-09 13:14:12

文章转自:http://blog.csdn.net/qq_29988575/article/details/54909213

6.0以下手机正常,6.0以上的却崩溃

解决方法:

targetSdkVersion 23以下的

private void callDirectly(String mobile) {
Intent intent
= new Intent();
intent.setAction(
"android.intent.action.CALL");
intent.setData(Uri.parse(
"tel:" + mobile));
mContext.startActivity(intent);
}

 

然后Manifest 加上权限就OK

<uses-permissionandroid:name="android.permission.CALL_PHONE" />

 

targetSdkVersion>=23的需要加以下代码

 
final public static int REQUEST_CODE_ASK_CALL_PHONE=123;
public void onCall(String mobile) {
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.CALL_PHONE);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mContext,
new String[] {
Manifest.permission.CALL_PHONE
}, REQUEST_CODE_ASK_CALL_PHONE);
return;
}
else {
// 上面已经写好的拨号方法 callDirectly(mobile);
}
}
else {
// 上面已经写好的拨号方法 callDirectly(mobile);
}

}
//动态权限申请后处理
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){
switch (requestCode) {
case REQUEST_CODE_ASK_CALL_PHONE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted callDirectly(mobile);
}else {
// Permission Denied Toast.makeText(MainActivity.this,"CALL_PHONE Denied", Toast.LENGTH_SHORT) .show();
}break;
default:super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} }