1

I use WebChromeClient for opening a link to news.html on my server, it opens safari and shows the content, ok.

But...

When I implent WebViewClient shouldOverrideUrlLoading to intercept the call when it's link to a .pdf file (use another class for it) the WebChromeClient link to news.html stays in webview and does not open safari anymore...

I am doing something wrong, but what?

Code snippet: (links are long so I shortened it)

myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);

//only to catch url override
myWebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.startsWith("http://www.domain.nl/pdf")){
        Intent i = new Intent();
                i.putExtra("url", url);
                i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                return true ;
                }
                else {
                    return false ;
                }
        }
});

myWebView.setWebChromeClient(new WebChromeClient());
//loading
myWebView.loadUrl("http://www.domain.nl/news.html");
Harry
  • 786
  • 1
  • 8
  • 27

3 Answers3

1

return false; means that the url wasn't handled and it should be opened in the WebView. If you want to open the url in an external browser you need to explicitly do it.

myWebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.startsWith("http://www.domain.nl/pdf")){
            Intent i = new Intent();
            i.putExtra("url", url);
            i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            return true ;
        }

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        return true;
    }
});
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
  • Thanks for responding, clear and usefull. This way all the links are opening in browser except the override pdf ones. But some links can stay in WebChromeClient so I thought about the contruction above. Use WebViewClient only to override but handle the rest in WebChromeClient. But that's not the way I guess... – Harry Mar 08 '13 at 13:25
  • can you explain why do you need to use `WebChromeClient`? – Vladimir Mironov Mar 08 '13 at 13:37
  • I use also GeoLocation in WebChromeClient. – Harry Mar 08 '13 at 14:31
0

To open the PDF file url :

add the "http://docs.google.com/viewerembedded=true&url=" string at starting of PDF file url string...

try this it work

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView webView;

    webView = new WebView(this);

    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setPluginsEnabled(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    if (webUrl.equals(XmlFileUrl.MemberDownloadAppForm)) {
        webUrl = "http://docs.google.com/viewerembedded=true&url=" + webUrl;
    }

    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl(url);

    setContentView(webView);
}
SAndroidD
  • 1,745
  • 20
  • 33
0

An example that helped me to do something similar Here

For geolocation add:

myWebView.getSettings().setGeolocationEnabled(true);

and :

private class MyWebChromeClient extends WebChromeClient {

  .........

 @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) 
    {
        // callback.invoke(String origin, boolean allow, boolean remember);              
        callback.invoke(origin, true, false);
    }
 }
Riad
  • 3,822
  • 5
  • 28
  • 39