文章转自:
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); } }