2

I am trying to implement a call function in my application I have the following code:

Intent phoneCall = new Intent(Intent.ACTION_CALL);
phoneCall.setData(Uri.parse("123456"));
startActivity(phoneCall);

I added in the manifest file the required permission, but it still gives me the following error:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException` less... (Ctrl+F1) 
This inspection looks at Android API calls and ensures that the correct type of resource is passed to an int-parameter expecting resources of a given type; it checks that APIs which expect an RGB color integer are passed actual colors rather than color resources; it checks that APIs which require a certain permission have the permission declared in the manifest; it checks that parameters expected to fall within a given range actually do; it checks that results of certain method calls are looked at by the caller, and so on.

Do you know how I can fix that ://

Tano
  • 609
  • 8
  • 23
  • 3
    From API 23 onward, it is required to check for permissions at runtime, since they can be revoked by the user: https://developer.android.com/training/permissions/requesting.html – Andrew Brooke Feb 04 '16 at 18:48
  • It's actually written on the error.. `code should explicitly check to see if permission is available (with "checkPermission") or explicitly handle a potential "SecurityException"` – Enrichman Feb 04 '16 at 18:49
  • 1
    The error message tells you exactly how to fix the problem. Read it. – Code-Apprentice Feb 04 '16 at 18:49
  • This is similar error but for location service but would give you an idea about what needs to be done. See http://stackoverflow.com/questions/32491960/android-check-permission-for-locationmanager – Rohit5k2 Feb 04 '16 at 18:50
  • 1
    I'm voting to close this question as off-topic because the question contains the answer. – Code-Apprentice Feb 04 '16 at 18:50
  • @AndrewBrooke thanks a lot !!! – Tano Feb 04 '16 at 18:51

2 Answers2

2

Since your Target Sdk version is 23, you need to ask users permissions at run time. Although you still need to include permissions in Manifest.xml.

Here are some awesome tutorials to get started

Tutorial 1

Tutorial 2

thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61
0
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
                    REQUEST_CODE_ASK_PERMISSIONS);
        } else {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "your number"));
            startActivity(intent);
        }


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Call Permission Granted..Please dial again.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Call permission not granted", Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
user2273146
  • 1,602
  • 2
  • 14
  • 27