Android JS calls each other

Source: Internet
Author: User

WebView equivalent to the browser in Android, based on WebKit development, you can browse Web files, support CSS Javas cript and HTML

1 webview.getsettings (). setjavascriptenabled (true); // Allow JS to execute

2 settings.setjavascriptcanopenwindowsautomatically (true);//Allow JS to execute
3 Webview.addjavascriptinterface (object, "name"); // adds an object of the Name= "name" to objects. Object If this is the

The HTML file that loads this machine is as follows:

file:///android_asset/teste.html loading a file under Project assets teste.html

file:///sdcard/index.html loading the index.html file under SDcard

1 webview.loadurl ("file:///android_asset/index.html"); // Note that the location of this resource is under a folder that is tied to Res. 

If we do not want to create a new WebView process when using WebView, let the program jump to jump to the following settings

1 webv.setwebviewclient (new2public       boolean  Shouldoverrideurlloading (WebView view, String URL) {3              view.loadurl (URL);   Click on the hyperlink to re-load the original process URL4              returntrue; 5      }6 });   

Calling Java methods in JavaScript

1. First bind a current Java object to a JavaScript, using the following method

1 webview.addjavascriptinterface (This, "Js2java"); // This is the current object, bound to the JS something above, the main Js2java scope is global. Once initialized, it can be run anywhere
1 addjavascriptinterface// The Java objects and methods to be bound in the method are to be run in another thread and cannot be run in the thread that constructs them, which is also the purpose of using handler. 
1 // map Java Object "Jsinvokeclass" to a JavaScript object named "Js2java" 2 // a way to invoke Java objects through "Window.js2java" in JavaScript 3 mwebview.addjavascriptinterface (new Jsinvokeclass (), "Js2java");
1 /** Web page JavaScript call interface * */ 2 class Jsinvokeclass {3      Public void Back () {4        activity.finish (); 5     }6 }
1 // JavaScript calls Java Object Example 2 <body onload= "Javascript:window.js2java.back ()" >

2. Define the called Java method


1 Public class Javajsdemo extends Activity {2 3 4 private WebView WebView;5 6 @Override7 8PublicvoidonCreate (Bundle savedinstancestate) {9 Ten super.oncreate (savedinstancestate); One  A Setcontentview (r.layout.main); -  -WebView =(WebView) Findviewbyid (; the  -Webview.getsettings (). setjavascriptenabled (true); -  -Webview.addjavascriptinterface ( This, "Js2java"); +  -Webview.loadurl ("file:///android_asset/index.html"); +  A     } at  -Publicvoidprintmsg (String msg) { -  -System.out.println ("----------msg:" +msg);  -  -     } in  -Publicvoidclickdowork () { to  +Webview.loadurl ("Javascript:dosomework ()"); -  the     } *  $}

HTML code:

12 34 5<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>6 7<title></title>8 9<script language= "javascript" type= "Html/text" >Ten  One             functiondosomework () { Adocument.getElementById ("Helloweb"). Innerhtml= "Hellowebview"; -  -             } the  -</script> -  - +  -<body onload= "JAVASCRIPT:WINDOW.JS2JAVA.PRINTMSG (' Hellowebview ')" > +  A<div id= "Hellowebview" > at  -<a onclick= "Window.js2java.clickDoWork" > - -<br> -Click me! -</a> in  -</div> to  +</body> -  the
1 Public class Javajswebviewdemo extends Activity {2 private WebView Mwebview; 3Private Handler Mhandler =NewHandler (); 4     5PublicvoidonCreate (Bundle icicle) {6 super.oncreate (icicle); 7 Setcontentview (R.layout.webviewdemo); 8Mwebview =(WebView) Findviewbyid (; 9WebSettings websettings =mwebview.getsettings (); TenWebsettings.setjavascriptenabled (true);  OneMwebview.addjavascriptinterface (NewObject () { APublicvoidclickdowork () { (NewRunnable () { -Publicvoidrun () { theMwebview.loadurl ("Javascript:dosomework ()");  -                     }     -                 });  -             }, 
Public     void printmsg (String msg) {             System.out.println ("----------msg:" + msg);        }  
mwebview.loadurl ("file:///android_asset/demo.html"22 }


Using Addjavascriptinterface under Android 4.2 is a security breach, this article can be used webview-js-java-interface-research/principle, to avoid the problem of this loophole, is completely JS level of the analytic call, and packaging more comprehensive, more thorough analysis!

Android JS calls each other

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: 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.