Android--------WebView+H5 development of imitating meituan preloading, loading failure and reloading

Android embedded development has occupied the majority, many interfaces are displayed in the form of web pages, WebView can make web pages easily embedded in the app, and can also directly call with js.

This blog mainly imitates the preloading of meituan's travel module, web page loading failure and web page reloading,

The effect is as follows:

              

The steps in the figure are: if the web page is loaded without network, it will not be loaded out, and a prompt interface will appear to open the network, and if it is refreshed, the interface can be loaded again,

In the above figure, when there is no network, the Android WebView native page loading failure interface will appear when you click refresh multiple times. This is a Bug.

 

The effect picture of my case is as follows:

         

 

Set access network permission in Android manifest.xml:

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

Related code:

    webview.setHorizontalScrollBarEnabled(false);//Horizontal not shown
        webview.setVerticalScrollBarEnabled(false); //Do not display vertically
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.supportMultipleWindows();  //Multiple windows
        webSettings.setAppCacheMaxSize(1024 * 1024 * 25);//Set buffer size, I set 8M
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setSavePassword(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setSaveFormData(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
      //Set cache
        if (!NetUtils.isNetworkAvailable(MainActivity.this)) {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        } else {
            webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        }
        webview.loadUrl("url");
private void showWebViewLoad(){
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                linearLayout.setVisibility(View.GONE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, 
                            String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                webviewError.setVisibility(View.VISIBLE);
            }
        });
    }

Complete relevant functions in corresponding methods;

 

Source download Github: https://github.com/DickyQie/android-webview-ui

Tags: Android network github xml

Posted on Fri, 03 Apr 2020 11:00:33 -0700 by Goose