Android WebView interacting with HTML5

Source: Internet
Author: User

Ext.: http://fangjie.info/?p=417#more-417

One, Webview.setwebviewclient (New Mywebviewclient ());

1.publicboolean  shouldoverrideurlloading (WebView view, String URL) {        onwebpageshouldload ( view, URL);   // through       return true ;  }

The click Request is a link is only called, overriding this method returns true to indicate that clicking on the link in the page or in the current WebView jump, do not jump to the browser side.

Pit Daddy 1:android 2.3.x webview Two funny bugs:http://blog.csdn.net/thestoryoftony/article/details/7844287

Workaround: add logic to the onpagestarted process.

2.publicvoid  onpagestarted (WebView view, String URL, Bitmap favicon) {}

Called when a page load is started.

The difference between shouldoverrideurlloading and onpagestarted:
When you click on the link in the page they will both execute, but return to the previous page when the onpagestarted will execute, but Shouldoverrideurlloading will not execute, that is, when the onpagestarted is executed

 Public void onpagefinished (WebView view, String URL) {       onwebpageloaded (view, URL);}

Called at the end of page loading.

Ii. webview.setwebchromeclient (New Mywebchromeclient ());

 public  void   Onreceivedtitle (WebView view, String title) { //   set Actionbar title    public  void  onprogresschanged (WebView view, int   progress) { //  set page load progress  } @Override  public  boolean  Onjsconfirm (WebView view, string URL, String message, final   Jsresult result) { //  popup box handling (Alert, Confirm) } 

Iii. Webview.addjavascriptinterface (jsobject, "jsobj");

1. Write an interface class first

 Public classjsinteface{//share the relevant contentPrivateString Mtitle;PrivateString Mdes;PrivateString MLink;PrivateString Mimgurl;PrivateString Mbigimgurl; @JavascriptInterface Public voidsetsharecontent (String title,string des,string link,string imgurl,string bigimgurl) {mtitle=Title; Mdes=Des; MLink=Link; Mimgurl=Imgurl; Mbigimgurl=Bigimgurl; LOG.I ("OUTPUT", "11title:" +mtitle+ "desc:" +mdes+ "MLink" +mlink+ "Mimgurl" +mimgurl+ "Mbigimgurl" +mbigimgurl);}}

2. Injecting an object from the interface class into the WebView
Webview.addjavascriptinterface (Jsobject, "jsobj");

3. Invoking the JS of the injected object

Mwebview.loadurl ("Javascript:window.jsObj.setShareContent" (document.getElementById (' App_title '). InnerHTML, "    + "document.getElementById (' App_desc '). InnerHTML,"    + "document.getElementById (' App_link '). InnerHTML,"    + "document.getElementById (' App_img_url '). SRC,"    + "document.getElementById (' App_big_img_url '). src)";

Pit Daddy 2:webview.addjavascriptinterface () does not work on API 17
Http://stackoverflow.com/questions/16353430/appview-addjavascriptinterface-does-not-work-on-api-17

Workaround: add @javascriptinterface before the interface method, and introduce the class, import android.webkit.JavascriptInterface;

Iv. Webview.setonkeylistener (New View.onkeylistener ()

Mwebview.setonkeylistener (NewView.onkeylistener () {@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) {                  if(event.getaction () = =Keyevent.action_down) {                      if(KeyCode = = Keyevent.keycode_back && mwebview.cangoback ()) {//means press the back keyMwebview.goback ();//back                         return true;//has been processed                    }                  }                  return false; }          });

This code is the Listen Back button to make WebView back a page instead of exiting WebView, similar to the fallback button in the browser.

V. Webview.setdownloadlistener (new Mydownloadlistener ());

This API can do the download aspect of the processing, you do not use in the project, here do not explain.

Public <textarea class="crayon-plain print-no" style="-moz-tab-size: 4; font-size: 14px ! important; line-height: 17.5px ! important; z-index: 0; opacity: 0; overflow: hidden; height: 376px;" readonly="" data-settings="dblclick">class jsinteface{//share related content private string Mtitle;private string Mdes;private string Mlink;private string Mimgurl;private string mbigimgurl; @JavascriptInterfacepublic void Setsharecontent (string title,string des,string Link , String imgurl,string bigimgurl) {mtitle=title; Mdes=des; Mlink=link; Mimgurl=imgurl; Mbigimgurl=bigimgurl; LOG.I ("OUTPUT", "11title:" +mtitle+ "desc:" +mdes+ "MLink" +mlink+ "Mimgurl" +mimgurl+ "</textarea> mbigimgurl" +mbigimgurl

Android WebView interacting with HTML5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.