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
310 views
in Technique[技术] by (71.8m points)

android - Force webview to display desktop sites

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?

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

Thanks

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


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

...