First, some common methods

1.WebChromeClient

WebChromeClient to deal with JS interaction and Internet industry content, commonly used methods are

  1. OnJsAlert () receives an alert event from a web page

  2. OnJsPrompt () receives the Prompt input box event for the web page

  3. OnJsConfirm () Receives the confirm confirmation event for the web page

  4. OnProgressChanged () page load percentage

  5. OnReceivedIcon () receives the icon of the web page

  6. OnReceivedTitle () Receives the title of the page

2.WebSettings

Webview setting class, can set various detailed parameters of webview

  1. SetAllowFileAccess (Boolean allow) setAllowFileAccess(Boolean allow) Specifies whether to use the file protocol to access web pages.

  2. SetBuiltInZoomControls (Boolean enabled) setBuiltInZoomControls(Boolean enabled)

  3. SetDatabaseEnabled (Boolean Flag) setDomStorageEnabled(Boolean Flag);

  4. SetDefaultFontSize (int size)

  5. SetJavaScriptEnabled (Boolean flag) setJavaScriptEnabled(Boolean flag)

  6. LOAD_DEFAULT (loaded from cache, not network), LOAD_CACHE_ELSE_NETWORK (loaded from cache, not network) LOAD_NO_CACHE LOAD_CACHE_ONLY setCacheMode()

3.WebView

Android provides controls that can load web pages and HTML data.

  1. Add an Object to webView to allow Java code to interact with JS addJavascriptInterface(Object Object, String Name)

  2. Load a web page, either a web page or a local web page, which is loaded using (file:///assets/). You can also call a javaScrpit method loadUrl(String URL)

  3. LoadData (String data, String mimeType, String encoding) loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)

  4. Reload ()

  5. Get the current page’s title getTitle()

  6. CanGoBack () goBack()

    2. Basic use of Webview

Because of the simplicity of the layout, I won’t show it here. There is only one VebView control in the layout

public class WebViewActivity extends Activity {


    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view); webView = (WebView)findViewById(R.id.webwiew); // Enable javaScript webView.getSettings().true); / / full-screen webView. GetSettings () setLayoutAlgorithm (WebSettings. LayoutAlgorithm. SINGLE_COLUMN); webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);


        webView.loadUrl("Here's the URL of the page I want to load."); // Handle URL redirection without throwing it into the system browser webview.setWebViewClient (new)WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,String url){
                view.loadUrl(url);
                return true; }}); } @Override public voidonBackPressed(){// If WebWiew can fall back to the previous pageif(webView.canGoBack()){ webView.goBack(); } super.onBackPressed(); }}Copy the code

Interaction between Java and JS in Android

  1. Start by writing a method (javaShow() method in my case) in the Activity. The @javascriptInterface tag must be posted above the method body
  2. Webview enables Java code to interact with JS by calling addJavascriptInterface(Object Object, String Name) to add a Java Object. Object is the Java Object passed to JS, and name is the name given to the Object.
  3. Js passes window. object name. Method name ()(I here the object name is demo, Java method name is javaShow, so can use window.demo to call the javaShow() method) can call the Java method
public class WebViewActivity extends Activity { WebView mWebView; // The content of the HTML String body; @Override @JavascriptInterface protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_web_view);

     mWebView = (WebView) findViewById(R.id.webview);
     mWebView.getSettings().setJavaScriptEnabled(true); // Javascript () can call the Java javaShow() method body = with window.demo.javashow ()"<html><head><style>img{width:100%}</style><script type='text/javascript'>function show(){window.demo.javaShow()} </script></head><body>"+ HTML code (HTML code is a few words instead of some HTML content because of space problems) +"</body></html>";
     mWebView.addJavascriptInterface(this,"demo");
     mWebView.loadDataWithBaseURL(null, body, "text/html"."utf-8", null);
 }
 
 @JavascriptInterface
 public void javaShow(){
     Intent intent = new Intent();
     intent.setClass(this,DetailImageActivity.class);
     intent.putExtra("image",images); startActivity(intent); }}Copy the code

Four, the use of WebView need to pay attention to the problem

A problem with android phones below version 4.2 is that webView will be injected with JavaScript, causing information security problems, such as downloading viruses and sending SMS messages. The problem is the addJavascriptInterface method. JavaScript can directly manipulate the native JAVA interface by calling this interface. Google solved this problem in version 4.2 by adding a declaration, @javascriptInterface, to methods called by JS, but there is no official solution for phones under version 4.2.