Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 825
Dinuka Jay Avatar asked Sep 26 '22 15:09

Dinuka Jay


1 Answers

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.

like image 157
Jörn Buitink Avatar answered Sep 28 '22 06:09

Jörn Buitink