Search code examples
javascriptandroidhtmlwebviewandroid-webview

Cant make voice calls from web page inside android webview


I have an application with a webView, which loads a list of contact details which users are allowed to make Voice calls by clicking that number. In the web page tel: tag is used.following is the code I have written.

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        final Uri uri = request.getUrl();
        if (uri.toString().startsWith("tel:"))
            makePhoneCall(uri.toString());
        return super.shouldOverrideUrlLoading(view, request);
    }

makePhoneCall()

public void makePhoneCall(String phoneNumber) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL);
    checkPermissions();
} else {
    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber)));
}

}

the issue is..

After installing on many devices, on some devices users, can't make the call, on clicking the number nothing happens. I am sure the touch event is happening because 5 seconds after clicking the number a pop up appears which works fine.

Some of the devices are Nokia, Asus Zenfone Max with android Oreo.

I have checked android versions most devices have 6+ android version.

  • I am running the webView on a different thread created from onCreate();

while on other devices everything works fine.

I can't seem to figure out what is going on here.


Solution

  • I would suggest you to use Intent.ACTION_DIAL instead of ACTION_CALL, you will need no call permission in your app while using ACTION_DIAL, it will just open your dialer with number already entered in it and will allow user to decide if they really want to call or not.

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String Url) {
        if (Url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(Url));
                startActivity(intent);
                return true;
        }
        return false;
    }