Force webview to display desktop sites

后端 未结 3 2096
悲&欢浪女
悲&欢浪女 2020-12-11 07:05

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

相关标签:
3条回答
  • 2020-12-11 07:47

    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.

    0 讨论(0)
  • 2020-12-11 07:50

    You Can try this

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setUserAgentString("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0");
    
    0 讨论(0)
  • 2020-12-11 07:55

    The only solution which worked for me (javascript will be executed many times, but this is the only working solution for now)

    @Override
    public void onLoadResource(WebView view, String url) {
           super.onLoadResource(view, url);
           view.evaluateJavascript("document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));", null);
    }
    

    You can set desktop UA string too

    webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
    
    0 讨论(0)
提交回复
热议问题