Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

android - How to make links open within webview or open by default browser depending on domain name?

I have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my application.

I tried to use public boolean shouldOverrideUrlLoading(WebView view, String url) but it does not work properly.

Here is the code that does not work:

public class MyWebViewClient extends WebViewClient {
    @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                   try {
                   URL urlObj = new URL(url);
                   if (urlObj.getHost().equals("192.168.1.34")) {
                       view.loadUrl(url);
                       return true;
                   } else {
                       view.loadUrl(url);
                       return false;
                     }
                   } catch (Exception e) {

                   }
               }
}

In both cases ( return true and return false) the URL is handled by my application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Once you create and attach a WebViewClient to your WebView, you have overridden the default behavior where Android will allow the ActivityManager to pass the URL to the browser (this only occurs when no client is set on the view), see the docs on the method for more.

Once you have attached a WebViewClient, returning false form shouldOverrideUrlLoading() passes the url to the WebView, while returning true tells the WebView to do nothing...because your application will take care of it. Unfortunately, neither of those paths leads to letting Android pass the URL to the browser. Something like this should solve your issue:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    try {
      URL urlObj = new URL(url);
      if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
        //Allow the WebView in your application to do its thing
        return false;
      } else {
        //Pass it to the system, doesn't match your domain
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
        //Tell the WebView you took care of it.
        return true;
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
}

I know that seems a little counterintuitive as you would expect return false; to completely circumvent the WebView, but this is not the case once you are using a custom WebViewClient.

Hope that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...