32

With the simple below code I can get my url loaded correctly, but, I get "ERR_UNKNOWN_URL_SCHEME" when trying to tap on html links that starts with mailto: whatsapp: and tg: (Telegram).

Anyone can help me to fix this please? Unfortunately I do not know Java at all :(

Thanks.

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
        
    public class MainActivity extends Activity {
    
        private WebView mWebView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
    
            // Force links and redirects to open in the WebView instead of in a browser
            mWebView.setWebViewClient(new WebViewClient());
    
            // Enable Javascript
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
    
            // Use remote resource
            mWebView.loadUrl("http://myexample.com");
        }
    }
emen
  • 6,050
  • 11
  • 57
  • 94
NGC7803
  • 341
  • 1
  • 3
  • 6

6 Answers6

23

You have to set a client in the webview and pass these to an intent

webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( URLUtil.isNetworkUrl(url) ) {
                return false;
            }
            if (appInstalledOrNot(url)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity( intent );
            } else {
                // do something if app is not installed
            }
            return true;
        }

    });
}

You can have a method to check if app is installed

private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }
Chris Gomez
  • 6,644
  • 4
  • 18
  • 39
  • 1
    Awesome thanks! This works! Can I ask you one more thing; If there is not WhatsApp or Telegram installed on the device the app crash with: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=tg://msg?text=My text to send via Telegram } Would be possible to add a check that if the package isn't installed show a popup with a warning message? – NGC7803 Jan 19 '17 at 09:13
  • Perfect! Thank you very very much @cgomezmendez! – NGC7803 Jan 22 '17 at 20:32
  • Seems `shouldOverrideUrlLoading` of this form is deprecated. Is it worth using the other one instead, when possible ? Also, I think you forgot to convert to Uri... – android developer May 27 '19 at 06:44
  • 3
    Any idea how to achieve the same in Flutter? – Alan Steiman Jun 18 '20 at 23:36
15

You need override the method shouldOverrideUrlLoading of WebViewClient in which you can control link transfer by yourself.

Because html links that starts with mailto: whatsapp: and tg: (Telegram). is not common url start with "http://" or "https://", so WebView cannot parse it to right place, we should use intent to redirect the url.

For example:

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);
                    return true;
                } catch (Exception e) {
                    Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e);
                    return true;
                }
            }

then setWebViewClient to your WebView, like this:

    public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                view.getContext().startActivity(intent);
                return true;
            } catch (Exception e) {
                Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e);
                return true;
            }
        }
        });

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Use remote resource
        mWebView.loadUrl("http://myexample.com");
    }}
emen
  • 6,050
  • 11
  • 57
  • 94
TTKatrina
  • 466
  • 5
  • 5
5
@Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {
            if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return false;
        }

Put this code in your mWebView.setWebViewClient(new WebViewClient(). it will be working perfectly for all link like tel:, whatsapp:, mailto: etc.

4

This Work For Me

"webview.setWebViewClient(new WebViewClient() { String currentUrl;

       @Override
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
           currentUrl = url;

           if (url.startsWith("http") || url.startsWith("https")) {
               return false;
           }
           if (url.startsWith("intent")) {




             try {
                 Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);

                 String fallbackUrl = intent.getStringExtra("browser_fallback_url");
               if (fallbackUrl != null) {
                   webview.loadUrl(fallbackUrl);
                   return true;
               }}

           catch (URISyntaxException e) {
               //not an intent uri
           }
   return true;//do nothing in other cases
Arooter
  • 43
  • 3
0

mailto links will not get loaded in your webview.You have check for it like this in shouldOverrideUrlLoading and handle it with intent.

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, message);
        startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));
        view.reload();
        return true;
    }
  }

Similar question Android Webview ERR_UNKNOWN_URL_SCHEME Error

Community
  • 1
  • 1
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
-1

this is for internal web browser.

you can open url with external app (like Chrome) for resolve this.

use url_handler package for opening a url with external app.

Ali Bagheri
  • 3,068
  • 27
  • 28