Enable only Desktop Mode in Android WebView

2019-09-02 01:12发布

I am trying to enable only Desktop mode of a website in android webview in android device. But it renders only in mobile view. Is there any way I can force the view to be only in Desktop mode?

Below is my Android webview code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_home);
    wb=(WebView)findViewById(R.id.webView1);
    CookieManager.getInstance().setAcceptCookie(true);
    wb.getSettings().setLoadWithOverviewMode(true);
    wb.getSettings().setUseWideViewPort(true);
    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setAllowFileAccess(true);
    wb.getSettings().setAllowContentAccess(true);
    wb.setScrollbarFadingEnabled(false);
    WebSettings settings = wb.getSettings();
    settings.setDomStorageEnabled(true);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);

    wb.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedSslError(WebView v, SslErrorHandler handler, SslError er){
            handler.proceed();
        }

        /*@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            } else {
                return false;
            }
        }*/
    });
    wb.loadUrl("https://cppt01.chartercom.com/pathtrak/direct/sa?node=30AA1E");
    wb.setVisibility(View.VISIBLE);
}

1条回答
够拽才男人
2楼-- · 2019-09-02 02:03

Try this :

 * private static final String desktop_mode = "Mozilla/5.0 (X11; Linux x86_64) 
   AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";

* private static final String mobile_mode = "Mozilla/5.0 (Linux; U; Android 4.4; en- 
  us; Nexus 4 Build/JOP24G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile 
  Safari/534.30";


 //Choose Mobile/Desktop mode.

 WebSettings settings = mWebView.getSettings();
 settings.setUserAgentString(desktop_mode );
查看更多
登录 后发表回答