简体   繁体   中英

Webview opening certain link in browser

I have an app that opens my webpages in the same intent and within the same webview. Now, I have a certain page that I want it to open in browser when user requests it. Is that possible? Here is my Code:

 WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.getSettings().setAllowFileAccessFromFileURLs(true);
    myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    myWebView.loadUrl("https://mywebpage.me");
    String url = myWebView.getUrl();

myWebView.setWebChromeClient(new WebChromeClient());

and if the user navigates to https//:mywebpage.me/About.html i want it to show in browser.

You should listen for navigation event.

  1. Create WebViewClient

     private class MyClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.equals(PUT_YOUR_URL_HERE)) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } return false; } } 
  2. Pass it to WebView

     myWebView.setWebViewClient(new MyClient()); 

By the way, this method was deprecated in API level 24.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM