How to handle intent:// on a webView URL?

前端 未结 2 973
南方客
南方客 2020-12-14 02:27

I\'m currently developing an android app with a webView. On some urls I need to use the shouldOverrideUrlLoading method, which is pret

相关标签:
2条回答
  • 2020-12-14 02:39

    Structure of this intent url is described here https://developer.chrome.com/multidevice/android/intents.

    So we can handle it in internal WebView like this:

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("intent://")) {
            try {
                Context context = view.getContext();
                Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    
                if (intent != null) {
                    view.stopLoading();
    
                    PackageManager packageManager = context.getPackageManager();
                    ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
                    if (info != null) {
                        context.startActivity(intent);
                    } else {
                        String fallbackUrl = intent.getStringExtra("browser_fallback_url");
                        view.loadUrl(fallbackUrl);
    
                        // or call external broswer
    //                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fallbackUrl));
    //                    context.startActivity(browserIntent);
                    }
    
                    return true;
                }
            } catch (URISyntaxException e) {
                if (GeneralData.DEBUG) {
                    Log.e(TAG, "Can't resolve intent://", e);
                }
            }
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-14 03:01

    Comrade's answer is the one to use.


    The way I managed to solve this isn't elegant but it works.

    First we check if the url startsWith with intent:// and contains scheme=http, if so, we get the value right after intent://everything until# and pass it to Intent.ACTION_VIEW, if not, we return false (ignore click).
    I've tested this solution with several results from google mobile search, such as, twitter, facebook, google maps and wikipedia and it worked flawlessly.

    @Override
    public boolean shouldOverrideUrlLoading( WebView view, String url) {
        if(url.startsWith("intent://") && url.contains("scheme=http")){
            url = Uri.decode(url);
            String bkpUrl = null;
            Pattern regexBkp = Pattern.compile("intent://(.*?)#");
            Matcher regexMatcherBkp = regexBkp.matcher(url);
            if (regexMatcherBkp.find()) {
                bkpUrl = regexMatcherBkp.group(1);
                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+bkpUrl));
                startActivity(myIntent);
                return true;
            }else{
                return false;
            }
       }
    return false;
    }
    

    If you've a better solution, I would like to hear it.
    Thank you all for the support, specially CommonsWare.

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