WebView第二次显示空白屏幕?

时间:2017-03-18 18:19:11

标签: java android android-fragments webview android-webview

我目前正在开发一款应用程序,可在Android WebView中以桌面模式加载https://web.whatsapp.com/ whatsapp for web。 它在棒棒糖版本上工作正常,但在API 23及以上版本(棉花糖及以上版本)上面,应用程序仅加载网站一次,如果按下重新加载按钮,它只显示一个空白屏幕!

这是Android API级别21上的代码正常工作但不是上面请帮助!!

public class Main_web_view_frag extends android.support.v4.app.Fragment
{

 public WebView Assignment_webView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View slide_view = inflater.inflate(R.layout.assignment_main_webview, container, false);

        Assignment_webView = (WebView)slide_view.findViewById(R.id.webView);

        Assignment_webView.getSettings().setJavaScriptEnabled(true);
        Assignment_webView.setWebViewClient(new My_web_Browser());
        Assignment_webView.loadUrl("https://web.whatsapp.com/");
        setDesktopMode(true);

        return slide_view;
    }


    private class My_web_Browser extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
             return true;
        }
    }

    public void setDesktopMode(final boolean enabled)
    {
        final WebSettings Assignment_webSettings = Assignment_webView.getSettings();

         String new_Assign_UserAgent;

        if (enabled)
        {
            new_Assign_UserAgent = Assignment_webSettings.getUserAgentString().replace("Mobile", "eliboM").replace("Android", "diordnA");
        }
        else
        {
            new_Assign_UserAgent = Assignment_webSettings.getUserAgentString().replace("eliboM", "Mobile").replace("diordnA", "Android");
        }


        Assignment_webSettings.getJavaScriptEnabled();
        Assignment_webSettings.setUserAgentString(new_Assign_UserAgent);

        Assignment_webSettings.setUseWideViewPort(true);
        Assignment_webSettings.setLoadWithOverviewMode(true);
        Assignment_webSettings.setSupportZoom(enabled);
        Assignment_webSettings.setBuiltInZoomControls(true);
        Assignment_webSettings.getJavaScriptCanOpenWindowsAutomatically();
        Assignment_webSettings.setDisplayZoomControls(false);

        Assignment_webSettings.setDatabaseEnabled(true);
        Assignment_webSettings.setDomStorageEnabled(true);
        Assignment_webSettings.setAllowFileAccess(true);
        Assignment_webSettings.setAppCacheEnabled(true);

        Assignment_webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        Assignment_webSettings.setAppCachePath(getActivity().getApplicationContext().getCacheDir().getPath());


        String appCachePath = getActivity().getCacheDir().getAbsolutePath();
        Assignment_webSettings.setAppCachePath(appCachePath);
    }


}

1 个答案:

答案 0 :(得分:1)

终于有了解决方案!!您只需要在webview中添加一个useragent字符串,就像chrome或任何其他Web浏览器一样。

yourWebView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");