0

In my application I open the browser. The web view should make a call when corresponding button is pressed.

For this I have added a permission to my manifest:

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

I user a following code in my activity:

 protected class MyWebClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
            startActivity(intent);
        }else if(url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
        }
        return true;
    }
}

This combination functioned well for 6 months. But today I have suddenly receives the error message:

Call requires permissions that may be rejected by user

I cannot undersatnd, what is actually required. I tryed to add an annotation @RequiresPermission, but it does not see CALL_PHONE from my manifest.

What should I add?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2957954
  • 1,221
  • 2
  • 18
  • 39

1 Answers1

2

This is because your compileSdkVersion = 23. For Marshmallow supported devices, you need to explicitly check to see if permission is available (with checkPermission).

So change the code as

 protected class MyWebClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:")) {
            if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.CALL_PHONE ) != PackageManager.PERMISSION_DENIED ) 
            {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
            startActivity(intent);
            }
        }
            else if(url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
        }
        return true;
    }
}
Droid Genie
  • 351
  • 4
  • 15