Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView err_unknown_url_scheme

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");
    }
}
like image 215
NGC7803 Avatar asked Jan 17 '17 09:01

NGC7803


People also ask

How can I solve the error net err_unknown_url_scheme in Android Webview?

You may get this "ERR_UNKNOWN_URL_SCHEME error" during mailto: or tel: links inside an iframe. To solve it, try to add target="_blank" in your URL Scheme/Code.

How do I fix Net :: err_unknown_url_scheme?

– Solve Net err_unknown_url_scheme Error: Open in New Window It is an easy and quick solution that involves editing the URL href code. The error can be solved in a roundabout way by adding target=”_blank” and enabling an undesirable effect of development as a new window opens now by this link.

What does this mean net :: Err_cleartext_not_permitted?

Opening URL inside any Android Application will use Android Webview and for some URLs, you might encounter ERR_CLEARTEXT_NOT_PERMITTED Error. The error should look similar to the below image. So what this exactly mean? Cleartext is any transmitted or stored information that is not encrypted or meant to be encrypted.


2 Answers

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;     } 
like image 59
Cristian Gomez Avatar answered Sep 27 '22 19:09

Cristian Gomez


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");
}}
like image 34
TTKatrina Avatar answered Sep 27 '22 20:09

TTKatrina