强制 webview 显示桌面站点 [英] Force webview to display desktop sites

查看:33
本文介绍了强制 webview 显示桌面站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 webview 客户端,当我们查看 URL 中不包含 google 一词的页面时,它会将用户代理设置为桌面浏览器.(也可以做其他事情,但一切正常).

I have the below webview client which sets the user agent to a desktop browser when we are viewing a page that does not contain the word google in the URL. (Also does other stuff but that all works fine).

 webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                     if (!url.contains("google")) {
                        String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
                        webView.getSettings().setUserAgentString(newUA);
                         view.loadUrl(url);  
                     }else {
                            webView.getSettings().setUserAgentString(null);
                            view.loadUrl(url);  
                     }
                    return true;
                }
                public void onPageStarted(WebView view, String url, Bitmap favicon)
                {
                    super.onPageStarted(view, url, favicon);
                    progressBar.setVisibility(View.VISIBLE);
                }
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                    String page = webView.getUrl();
                    if (!(page.contains("google"))){
                        grabit.setVisibility(View.VISIBLE);

                    }else{
                        grabit.setVisibility(View.GONE);

                    }
                    webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('"+CFG.Bookmarklet+"');");
                    progressBar.setVisibility(View.INVISIBLE);
                    if (webView.canGoBack()){
                        left.setImageResource(R.drawable.ic_arrowleft);
                    }else{
                        left.setImageResource(R.drawable.ic_arrowleft_gray);
                    }
                    if (webView.canGoForward()){
                        right.setImageResource(R.drawable.ic_arrowright);
                    }else{
                        right.setImageResource(R.drawable.ic_arrowright_gray);
                    }

                }
            });

这个问题是,虽然在某些站点上它可以完美运行,但在其他站点上却不起作用,而在某些站点上,它似乎只是更改了视口.几个例子是:

This issue with this is that while on some sites it works perfectly others it does not work and on some it seems to just change the view port. A few examples are:

> Argos - shows mobile
> Tesco - shows mobile but view port has changed 
> Amazon - works 
> John Lewis - shows mobile but view port has changes
> Play.com - works

所以我有什么遗漏吗?它不工作的网站的另一种方式是检查浏览器来决定显示什么?

So is there something I am missing? Another way the websites it does not work on are checking the browser to decide what to display?

Chrome 中的显示桌面版本"似乎适用于这些网站.. 那么 chrome 可能会做其他事情吗?

It would seem that the 'show desktop version' in Chrome works fine for these sites.. so prehaps chrome does something else to?

谢谢

推荐答案

你可以使用 setDesktopMode(true) from 这个 WebView 子类 或阅读它是如何详细实施.

You can use setDesktopMode(true) from this WebView subclass or read how it's implemented in detail.

这样,您就不必设置固定的用户代理字符串.此外,仅设置用户代理字符串通常是不够的.当然,这取决于网站,因为每个网站都使用自己的方式来确定客户端是移动设备还是桌面设备.

This way, you don't have to set a fixed user-agent string. Moreover, setting the user-agent string only is usually not enough. It depends on the site, of course, since every website uses their own way of determining whether the client is on mobile or desktop.

这篇关于强制 webview 显示桌面站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆