Android open intent:// protocol in WebView

前端 未结 1 1134
醉话见心
醉话见心 2021-01-13 21:55

When i load url into WebView and try to open links, some of them shows an error page like:

net::ERR_UNKNOWN_URL_SCHEME intent://maps.yandex.ru?utm_med

1条回答
  •  时光说笑
    2021-01-13 22:31

    I found a solution. This question and this documentation helps me to understand situation.

    As a result, i've written a link handler which follow this logic:

    1. Open http and https in the same WebView
    2. Tries to handle known schemes (tel: etc.)
    3. If can't find a known activity, parses intent: scheme and tries to run appropriate app
    4. If required app not installed, tries to load provided fallback url into current WebView
    5. If no fallback url provided, redirects to market and ask to install required app

    The code is:

    mWebView.setWebViewClient(new CustomWebViewClient());
    
    //...
    
    private class CustomWebViewClient extends WebViewClient {
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            if (url.startsWith("http")) return false;//open web links as usual
            //try to find browse activity to handle uri
            Uri parsedUri = Uri.parse(url);
            PackageManager packageManager = getActivity().getPackageManager();
            Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
            if (browseIntent.resolveActivity(packageManager) != null) {
                getActivity().startActivity(browseIntent);
                return true;
            }
            //if not activity found, try to parse intent://
            if (url.startsWith("intent:")) {
                try {
                    Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                        getActivity().startActivity(intent);
                        return true;
                    }
                    //try to find fallback url
                    String fallbackUrl = intent.getStringExtra("browser_fallback_url");
                    if (fallbackUrl != null) {
                        webView.loadUrl(fallbackUrl);
                        return true;
                    }
                    //invite to install
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(
                            Uri.parse("market://details?id=" + intent.getPackage()));
                    if (marketIntent.resolveActivity(packageManager) != null) {
                        getActivity().startActivity(marketIntent);
                        return true;
                    }
                } catch (URISyntaxException e) {
                    //not an intent uri
                }
            }
            return true;//do nothing in other cases
        }
    
    }
    

    Maybe it needs some cleanup, but it can be helpful. Please tell me if you know an easier way of doing this, i still looking for the best solution.

    0 讨论(0)
提交回复
热议问题