简体   繁体   中英

How do I open another app in my WebView app?

I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files. When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.

But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.

Is it even possible?

To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;

   WebView webView = (WebView) findViewById(R.id.infoView);

   webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                // Assuming you are giving link to some PDF file.
                if (url.contains(".pdf")) {
                    // Now do what you want to with the url here
                }

                return true;
            }
    }

This way, you can intercept any link tapped in WebView and then do whatever you want.

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