Jay
Jay

Reputation: 5074

Calling number from dialpad returns warning: "Call requires permission which may be rejected by user"

I am trying to basically dial a number when the user clicks on a TextView that has a number:

number_title.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:+"+user.getTelephone()));
                        activity.startActivity(callIntent);
                        //the above line returns the warning given below
                    }
});

The warning I get:

Call requires permission which may be rejected by user. Code to explicitly check to see if the permission is available

I understand that the app needs to request permission if the permission hasn't been granted before. My question is, how do I request the permission explicitly before making the call?

Upvotes: 5

Views: 3676

Answers (3)

V_J
V_J

Reputation: 1081

This is just a sample of reference call for checking the permission:

if(!checkSelfPermission(Manifest.permission. CALL_PHONE)==PackageManager.PERMISSION_GRANTED){

    requestPermissions(this,new String {}(Manifest.permission. CALL_PHONE),INT_TO_CHECK RESULT);

}

Let me know if this works.

Edited: Made changes in the code to explain.

Upvotes: 2

Jörn Buitink
Jörn Buitink

Reputation: 2916

Well, this is about runtime permission, so only declaring them in manifest won't work. Instead you have to check the permission right before you start the call.

Something like this should work - the user will only be asked for the permission if he hasn't granted it before (or if he revoked it):

private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1234;

public void yourfunction() {
    number_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (ContextCompat.checkSelfPermission(thisActivity,
                                                Manifest.permission.CALL_PHONE)
                                != PackageManager.PERMISSION_GRANTED) {

                        ActivityCompat.requestPermissions(activity,
                            new String[]{Manifest.permission.CALL_PHONE},
                            MY_PERMISSIONS_REQUEST_CALL_PHONE);
                    } else {
                        executeCall();
                    }
                }
    });
}

private void executeCall() {
    // start your call here
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:+"+user.getTelephone()));
            activity.startActivity(callIntent);         
        }
    });
}


@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                executeCall();


            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
    }
}

I took the above code mainly from google: http://developer.android.com/training/permissions/requesting.html Visit the link if you want to have more information.

Edit: onRequestPermissionsResult will be a callback function in your activity. You may have to handle the call there.

Upvotes: 2

Ankur1994a
Ankur1994a

Reputation: 2122

use this may this will work :

 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Upvotes: 0

Related Questions