首先在AndroidMenifest文件里加上这个权限: <uses-permission android:name="android.permission.CALL_PHONE" />
1.如果是直接跳转到拨号盘,是不存在权限管制的问题的. 2.如果是直接拨打电话,会存在权限动态获取的问题. 解决的代码如下:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { //没有权限,申请授权 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { //如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true. // 如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false. // 如果设备策略禁止应用拥有这条权限, 这个方法也返回false. // 弹窗需要解释为何需要该权限,再次请求授权 Util.showToast(MyRevationDetailActivity.this, getResources().getString(R.string.call_phone_authority_tv)); //跳转到该应用的设置界面,让用户手动授权 Intent intentSet = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intentSet.setData(uri); startActivity(intentSet); } else { //直接请求授权 /* 在Fragment中申请权限,不要使用ActivityCompat.requestPermissions, 直接使用Fragment的requestPermissions方法,否则会回调到Activity的onRequestPermissionsResult */ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, Util.HTTP_CALL_PHONE_REQUEST); } // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, Util.HTTP_CALL_PHONE_REQUEST); } else { //在这里直接拨打电话 Intent intent = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse("tel:" + "电话号码"); intent.setData(data); startActivity(intent); }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); //requestCode 是一个标识, if (requestCode == Util.MY_PERMISSIONS_REQUEST_CALL_PHONE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { //获取到权限之后,进行下一步操作 startTakePhoto(); } else { //跳转到该应用的设置界面,让用户手动授权 Intent intentSet = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intentSet.setData(uri); startActivity(intentSet); Util.showToast(PhotoSelectorActivity.this, getResources().getString(R.string.allow_take_photo)); } } }