How to make a phone call using intent in Android without permission?

4.9k Views Asked by At

My app uses the android permission action_dial. How to change the code so it doesn't need permission? i need to be able to call witout permission

this is the code:

Its not my fault this post is full of code.


    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // Older Version No need to request Permission
        String dial = "tel:" + phoneNo;
        fragment.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(dial)));
    } else {
        // Need to request Permission
        if (fragment.getActivity() != null) {
            if (ContextCompat.checkSelfPermission(fragment.getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                fragment.requestPermissions(new String[]{
                        Manifest.permission.CALL_PHONE
                }, Constants.REQUEST_CODE__PHONE_CALL_PERMISSION);
            } else {
                String dial = "tel:" + phoneNo;
                fragment.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(dial)));
            }
        }
    }
}


                                             int[] grantResults, Fragment fragment, String phoneNo) {
    if (requestCode == Constants.REQUEST_CODE__PHONE_CALL_PERMISSION) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            callPhone(fragment, phoneNo);
        } else {
            Utils.psLog("Permission not Granted");
        }


    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // Older Version No need to request Permission
        String dial = "tel:" + phoneNo;
        activity.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
    } else {
        // Need to request Permission
        if (activity != null) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                activity.requestPermissions(new String[]{
                        Manifest.permission.CALL_PHONE
                }, Constants.REQUEST_CODE__PHONE_CALL_PERMISSION);
            } else {
                String dial = "tel:" + phoneNo;
                activity.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
            }
        }
    }

3

There are 3 best solutions below

2
On

You could do it like so:

Intent intentDial = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "123456789"));
context.startActivity(intentDial);

But it will navigate user to the default call dialer app and pass it the phone number so the user should push the call button to make a call. It doesn't make a call directly.

0
On

Direct Call cant be made without permission. You can open the dialer on top of the app with the needed number, the user should next click on the green call button. To do this use this code

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumberStr));

startActivity(callIntent);
1
On

You don't need permission to execute that phone call intent.

/**
 * Make a phone call. Send to the phone app
 *
 * @param phoneNumber the phone number to call
 */
private fun performPhoneCall(phoneNumber: String) {
    val intent = Intent(Intent.ACTION_DIAL)
    intent.data = Uri.parse("tel:$phoneNumber")
    try {
        context.startActivity(intent)
    } catch (ex: Exception) {
        displayErrorMessage()
    }
}