如何在WebView应用程序中打开另一个应用程序?

时间:2021-10-03 06:06:50

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.

我有一个Android应用程序,它显示一个移动网站(WebView),在移动网站上有链接重定向到PDF,Excel和视频文件。当我尝试在我的常规浏览器中打开它时,我的手机要求用另一个应用程序打开它或者它开始下载,所以我可以在之后打开它。

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

但是在我的WebView应用程序中,它不起作用,没有响应或显示“页面不可用”错误。

Is it even possible?

它甚至可能吗?

1 个解决方案

#1


5  

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

要在WebView中处理链接,可以使用WebViewClient类的shouldOverrideUrlLoading方法。考虑以下示例;

   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.

这样,您可以拦截在WebView中点击的任何链接,然后执行您想要的任何操作。

#1


5  

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

要在WebView中处理链接,可以使用WebViewClient类的shouldOverrideUrlLoading方法。考虑以下示例;

   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.

这样,您可以拦截在WebView中点击的任何链接,然后执行您想要的任何操作。