Reference: http://blog.csdn.net/wangtingshuai/article/details/8635787
JS Code
Inject JS function to listen to private void Addimageclicklistner () {//This function of JS function is to traverse all the IMG points, and add the onclick function, The function is to invoke the local Java interface when the image is clicked and pass the URL past Contentwebview.loadurl ("javascript: (function () {" + "var Objs = document.getElementsByTagName (\ "img\"); "+" for (var i=0;i<objs.length;i++) "+" {"+" objs[i].onclick=function () "+" { "+" Window.imagelistner.openImage (THIS.SRC); " + " } " + "}" + "})()");}
Filter pictures
//inject JS function to monitor Private voidAddimageclicklistner () {//The function of this JS function is to traverse all of the IMG, and add the OnClick function, function is to call the local Java interface when the image is clicked and pass the URL pastContentwebview.loadurl ("javascript: (function () {" + "var objs = document.getElementsByTagName (\" img\ ");" + "for (Var i=0;i<objs.length;i++)" + "{" + "if (OBJS[I].SRC)" +"if (objs[i].src.indexof (' Display ') >0)"+" objs[i].onclick=function () {"+" window.imagelistner.openImage (THIS.SRC); " + " } " + "}" + "})()"); }
Full code
Packagecom.example.testandroid;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.util.Log;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient; Public classMainactivityextendsactionbaractivity {WebView contentwebview; @SuppressLint ("Javascriptinterface") @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Contentwebview=(WebView) Findviewbyid (R.id.webview); //Enable JavaScriptContentwebview.getsettings (). setjavascriptenabled (true); //Just looking for a website with a picture.Contentwebview.loadurl ("http://www.qdaily.com/display/articles/7237"); //Add the JS interface class and Alias ImagelistnerContentwebview.addjavascriptinterface (NewJavascriptinterface ( This), "Imagelistner"); Contentwebview.setwebviewclient (Newmywebviewclient ()); } //inject JS function to monitor Private voidAddimageclicklistner () {//The function of this JS function is to traverse all of the IMG, and add the OnClick function, function is to call the local Java interface when the image is clicked and pass the URL pastContentwebview.loadurl ("javascript: (function () {" + "var objs = document.getElementsByTagName (\" img\ ");" + "for (Var i=0;i<objs.length;i++)" + "{"//+ "Window.imagelistner.openImage (objs[i].src)"+ "objs[i].onclick=function ()"+"{"+"window.imagelistner.openimage(this.src); " + " } " + "}" + "})()"); } //JS Communication Interface Public classJavascriptinterface {Privatecontext Context; PublicJavascriptinterface (Context context) { This. Context =context; } Public voidopenimage (String img) {LOG.E ("MyTag", IMG); } Public voidlog (String s) {LOG.E ("MyTag", s); } } //Monitor Private classMywebviewclientextendswebviewclient {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {return Super. shouldoverrideurlloading (view, URL); } @Override Public voidonpagefinished (WebView view, String URL) {view.getsettings (). setjavascriptenabled (true); Super. onpagefinished (view, URL); //after the HTML loading is complete, add the Click JS function of the listening image.Addimageclicklistner (); } @Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {view.getsettings (). setjavascriptenabled (true); Super. onpagestarted (view, URL, favicon); } @Override Public voidOnreceivederror (WebView view,intErrorCode, string description, String failingurl) { Super. Onreceivederror (view, ErrorCode, description, Failingurl); } }}
Let's do the pictures in Openimage.
WebView usage vs. js interaction (2) in response to a picture click event in WebView