Sushant
Sushant

Reputation: 254

I am checking call permission in marshmallow

public void checkPermissions() {

    Log.i(TAG, " Checking permissions.");

    // Verify that all required contact permissions have been granted.
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

        // Contacts permissions have not been granted.
        Log.i(TAG, "Call Phone permissions has NOT been granted. Requesting permissions.");
        requestPermissions();

    } else {

        // Contact permissions have been granted. Show the contacts fragment.
        Log.i(TAG,
                "Contact permissions have already been granted");
        showContactDetails();
    }
}


public void customDialog(Context ctx) {

    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(ctx, android.R.style.Theme_Material_Light_Dialog_Alert);
        builder.setMessage("Call permissions are needed to demonstrate access").setNeutralButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int whichButton) {
                        ActivityCompat.requestPermissions(CallChoosyActivity.this, Permissions, REQUEST_CALL_PHONE);
                    }
                });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    } else {
        builder = new AlertDialog.Builder(ctx);
        builder.setMessage("Call permissions are needed to demonstrate access").setNeutralButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int whichButton) {

                    }
                });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

}

public void requestPermissions() {

    // BEGIN_INCLUDE(contacts_permission_request)
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {

        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // For example, if the request has been denied previously.
        Log.i(TAG, "Displaying call permission rationale to provide additional context.");

        customDialog(CallChoosyActivity.this);
    } else {
        // Contact permissions have not been granted yet. Request them directly.
        ActivityCompat.requestPermissions(this, Permissions, REQUEST_CALL_PHONE);
    }
    // END_INCLUDE(contacts_permission_request)
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {

    if (requestCode == REQUEST_CALL_PHONE) {
        Log.i(TAG, "Received response for contact permissions request.");

        // We have requested multiple permissions for contacts, so all of them need to be
        // checked.
        if (PermissionUtil.verifyPermissions(grantResults)) {
            // All required permissions have been granted, display contacts fragment.
            showContactDetails();
        } else {
            Log.i(TAG, "Contacts permissions were NOT granted.");
        }

    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

public void showContactDetails() {
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + PartyTabsActivity.numbersList.get(0)));
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    startActivity(callIntent);
}

When I run app its showing this in logcat

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=android/com.android.internal.app.ResolverActivity } from ProcessRecord{2dd511f 24656:com.marg.pharmanxt/u0a158} (pid=24656, uid=10158) with revoked permission android.permission.CALL_PHONE at android.os.Parcel.readException(Parcel.java:1599) at android.os.Parcel.readException(Parcel.java:1552) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2771)

Upvotes: 0

Views: 7918

Answers (1)

Vivek Mishra
Vivek Mishra

Reputation: 5705

Change your permission request code according to this.

int checkPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    REQUEST_CALL_PHONE);
        } else {
            customDialog(CallChoosyActivity.this);
        }

Leave your onRequestPermissionsResult as it is

Upvotes: 2

Related Questions