首先在AndroidMenifest文件里加上這個權限: <uses-permission android:name="android.permission.CALL_PHONE" />android
1.若是是直接跳轉到撥號盤,是不存在權限管制的問題的. 2.若是是直接撥打電話,會存在權限動態獲取的問題. 解決的代碼以下:app
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)); } } }