开发者

Disable the automatic Linkify of Webview

开发者 https://www.devze.com 2023-02-20 17:20 出处:网络
I have a webview that I am creating.It seems to automatically be linkifying numbers into tel: urls.I didn\'t see a way to remove this ability (at least nothing similar to the way to enable it on a tex

I have a webview that I am creating. It seems to automatically be linkifying numbers into tel: urls. I didn't see a way to remove this ability (at least nothing similar to the way to enable it on a textview).

The code is pretty simple:

// populate the web view
WebView webView = (WebView) findViewById(R.id.app_info_webview开发者_Python百科);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);

webView.setBackgroundColor(0);
String url = APP_INFO_BODY_HTML + "?versionName=" + versionName;

webView.loadUrl(url);

I have a copyright notice at the bottom of the page, android is changing the 2011 into a clickable link that opens the dialer. Also, the App version 1.0.0 opens in the dialer.

Is there a way to disable this functionality?

Update: I just discovered that this seems device dependent...happens on the Droid X, but not a Samsung Captivate, not on Nexus S, and not the emulator.


There is a way to do that - rather ugly, two layered, but still a workaround.

You should

  1. modify how the webview will handle the auto-linkifiable items
  2. explicitly tell the loaded page not to apply styles and haptic feedback.

    mWebView.setWebViewClient( new WebViewClient() {
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, final String url) {
        Uri uri = Uri.parse(url);
    
        //TODO analyse the uri here 
        //and exclude phone and email from triggering any action
    
        return false;
    }
    
    public void onReceivedError(WebView view, int errorCode, 
                                            String description, String failingUrl) {}
    
    public void onPageFinished (WebView view, String url) {...}
    
    public void onPageStarted(WebView view, String url, Bitmap favicon) {...}
    
    public void onLoadResource(WebView view, String url) {...}
    }); 
    

    In the html specify the following meta tags inside the tag:

    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    

Hope this helps.


I'm surprised to see it is launching Dialer on selecting a number from your own WebView.

Unless, you override WebViewClient::shouldOverrideUrlLoading() and detect the url scheme has "tel" and start Dialer activity, it will never launch a Dialer from your WebView. Are you sure you are not handling tel: scheme in shouldOverrideUrlLoading()?


You can find the code that do the real detection in external/webkit/WebKit/android/nav/CacheBuilder.cpp : FindPartialNumber() and so on.

You can disable it or change the logic as you like.

0

精彩评论

暂无评论...
验证码 换一张
取 消