First quote an article, read this article basically understand how Android roughly and JS is how to interact
Getting Started with Android and Html+js interaction
----------------------------Split Line-----------------------------------
First of all to know what JS is, JS is equivalent to the function method within the HTML, all called JavaScript
So to interact, the two must coexist together, how to open a page in Android, the more common way is to use a WebView control, use it to load the Web page
Interaction is also bound to be both sides, accumulate, the following two parts of the explanation
PS: Here directly invoke the example in the previous article, slightly modified
1,android in the call JS
So the first must have JS, otherwise how to call, so the HTML is as follows
<HTML><Head><Metahttp-equiv= "Content-type"content= "text/html;charset=gb2312"><Scripttype= "Text/javascript">functionJavacalljs () {document.getElementById ("content"). InnerHTML= "<br\>java called the parameterless function of JS";}functionJavacalljswith (Arg) {document.getElementById ("content"). InnerHTML= ("<br\>"+arg);}</Script></Head><Body>HTML Content Display<BR/><H1><DivID= "Content">Content display</Div></H1></Body></HTML>
It is important to note that the names of the two methods need to be the same as the names that are called later: Javacalljs,javacalljswith
Then put it into the project, or deploy it on the server, and then load the URL in WebView
This only writes the code that WebView loads the URL.
Contentwebview = (WebView) Findviewbyid (R.id.webview); // Enable JavaScript Contentwebview.getsettings (). setjavascriptenabled (true); // loading HTML from the assets directory
This is because the author uses the method of putting the project in, so the Loadurl method passes a local relative address
Here WebView loading the Web page is finished, then how to use WebView call the JS it
Contentwebview.loadurl ("Javascript:javacalljs ()");
Or is it
Contentwebview.loadurl ("Javascript:javacalljswith (" + "' Http://blog.csdn.net/Leejizhou '" + ")");
Both are pre-defined in the HTML of the JS function name, an argument without a parameter (here for respect to cite the author, the blog address in the parameter is not modified)
To this, the Android calls JS to complete the
So interactive, in turn, how to interact with Android within HTML code
Modify the HTML code
<HTML><Head><Metahttp-equiv= "Content-type"content= "text/html;charset=gb2312"></Head><Body>HTML Content Display<BR/><H1><DivID= "Content">Content display</Div></H1><BR/><inputtype= "button"value= "Click Invoke Java Code"onclick= "window.android.startFunction ()" /><BR/><inputtype= "button"value= "Click Invoke Java code and pass parameters"onclick= "window.android.startFunction (' Http://blog.csdn.net/Leejizhou ')" /></Body></HTML>
As can be seen here, just as learning Android, in the XML page to write the onclick to bind the point-and-click Method, HTML is the same method, but the difference is that the input is not only a method name, but window. Variable name. Method Name
Method name from needless to say, as to what is the variable name, first look at the following code
First, the initialization of the webview is changed to the following code
Contentwebview = (WebView) Findviewbyid (R.id.webview); Enable JavaScript contentwebview.getsettings (). Setjavascriptenabled (true); Load HTML contentwebview.loadurl ("file:///android_asset/web.html") from the assets directory; Contentwebview.addjavascriptinterface (Mainactivity.this, "Android");
The last line to add the JS interface, the back of "Android" is the name of the previously said variable
And then down, you need to define this method.
//For security reasons targetsdkversion>=17 need to add @JavascriptInterface//JS calls the Android Java method name and the name corresponding to the alias after the button onclick in HTML@JavascriptInterface Public voidstartfunction () {Runonuithread (NewRunnable () {@Override Public voidrun () {Toast.maketext (mainactivity. This, "Show", 3000). Show (); } }); } @JavascriptInterface Public voidStartfunction (FinalString text) {Runonuithread (NewRunnable () {@Override Public voidrun () {NewAlertdialog.builder (mainactivity. This). Setmessage (text). Show (); } }); }
To do this, JS can call the local Android method, which enables the process of interaction
Of course, the interaction here is very simple, if the requirements are different, but also need to modify, such as the use of JS callevaluateJavascript()会使得网页不刷新并且较为容易获得返回值等等
This is very thorough in this article, and here is a direct reference to
Android: The way you want to interact with JS is here WebView
Android and JS Interaction