Example of interaction between Android and JavaScript (1)

Source: Internet
Author: User

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"> 

 

Related Article

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.