Mainactivity is as follows:
Package CN. testjavascript; import android. OS. bundle; import android. webKit. webchromeclient; import android. webKit. webview; import android. app. activity;/*** demo Description: * JavaScript calls the method in Android *, that is, click the button in HTML to call the method in Android ** reference: * http://blog.csdn.net/r8hzgemq/article/details/8480390 */public class mainactivity extends activity {private webview mwebview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {mwebview = (webview) findviewbyid (R. id. webview); initwebviewsettings (); // load the local HTML file mwebview. loadurl ("file: // android_asset/error.html "); // implement interaction between Android and JavaScript // note the second parameter in the addjavascriptinterface method // It represents the alias of our Java object javaclass. in this way, JS can use this alias to call the method in Android // that is, JS Code In: // window. js_invoke.gonetsetting (); // window. js_invoke.reload (); javaclass = new javaclass (mainactivity. this, mwebview, "http://www.ifeng.com/"); mwebview. addjavascriptinterface (javaclass, "js_invoke");} private void initwebviewsettings () {mwebview. setverticalscrollbarenabled (false); mwebview. sethorizontalscrollbarenabled (false); mwebview. getsettings (). setjavascriptenabled (true); mwebview. getsettings (). setsuppzoom zoom (true); mwebview. getsettings (). setdomstorageenabled (true); mwebview. getsettings (). setpluginsenabled (true); mwebview. requestfocus (); mwebview. getsettings (). setusewideviewport (true); mwebview. getsettings (). setloadwithoverviewmode (true); mwebview. getsettings (). setsuppzoom zoom (true); mwebview. getsettings (). setbuiltinzoomcontrols (true);} private class testjswebchromeclient extends webchromeclient {@ overridepublic void onprogresschanged (webview view, int newprogress) {super. onprogresschanged (view, newprogress); system. out. println ("loading newprogress =" + newprogress );}}}
Javaclass is as follows:
Package CN. testjavascript; import android. app. activity; import android. content. context; import android. content. intent; import android. webKit. webview; public class javaclass {private context; private string URL; private webview; Public javaclass (context, webview, string URL) {This. context = context; this. webview = webview; this. url = URL;} public void gonetsetting () {(activity) This. context ). startactivityforresult (new intent ("android. settings. wireless_settings "), 0);} public void reload () {This. webview. loadurl (this. URL );}}
Main. XML is as follows:
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <webview Android: id = "@ + ID/webview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: layout_centerinparent = "true"/> </relativelayout>
Error.html is as follows:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">