Android WebView err_unknown_url_scheme

后端 未结 5 698
花落未央
花落未央 2020-11-27 05:11

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: wh

5条回答
  •  情深已故
    2020-11-27 06:00

    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;
        }
    

提交回复
热议问题