http://my.oschina.net/u/1376187/blog/172296
The project uses the WebView Display Web page, which requires Web pages and native methods to interact, to search for an article, to forward sharing:
=============================================================================================================== ===============
Reprint please indicate the source is very handsome mobile development column http://blog.csdn.net/wangtingshuai/article/details/8631835 in the development process of Android, there are many times need to use the local Java generation Code to interact with JavaScript. Android on the interaction is very good encapsulation, in the development we can easily use Java code to call WebView in the JS, you can also use JS in the WebView to invoke the local Java code, so that we can achieve a lot of the original function, For example, click on the phone number on the page, the phone automatically dial the phone, click on the Web joke, automatically send text messages and so on. Needless to say, the goal of this tutorial is as follows
- Java code in Android calls WebView inside the JS script
- The JS script in WebView calls the local Java code
- Java calls JS and passes parameters
- JS calls Java and passes parameters
Function one Android call WebView in the JS script is very convenient, only need to call WebView Loadurl method (note to turn on JS support)
[HTML] View plain copy
- Enable JavaScript
- Contentwebview.getsettings (). Setjavascriptenabled (True);
- Loading HTML from the assets directory
- Contentwebview.loadurl ("file:///android_asset/wst.html");
- No parameter calls
- Contentwebview.loadurl ("Javascript:javacalljs ()");
function two
WebView JS calls The local Java method, this feature is slightly cumbersome to implement, but it is not very complicated, first to WebView binding Javascriptinterface,js script through this interface to invoke Java code. [Java] View plain copy
- Contentwebview.addjavascriptinterface (This, "WST");
Javainterface is actually a common Java class, which is the Java code we implement locally, passing object to WebView and assigning an alias, so that the JS script can call our method through the alias we give, in the code above, This is an instantiated object, WST is the alias of this object in JS
Function three
Java code calls JS and passes parameters
Only need to use the JS function to add parameters, the following is the case of passing a parameter, the need for a number of parameters of the time of their own stitching and line, note that the STR type in the pass time parameters to be enclosed in single quotation marks
[Java] View plain copy
- Mwebview.loadurl ("Javascript:test ('" + aa+ "')"); AA is the parameter of the JS function test ()
Function Four
JS call Java functions and pass parameters, Java function Normal writing, in the JS script when called a little attention
Then in the HTML page, the following code can be used to implement the call [HTML] view plain copy
- <div id= ' B ' ><a onclick= "window.wst.clickOnAndroid (2)" >b.c</a></div>
Here is an example of the implementation of the above function here is the HTML code of the instance, loaded from the Assert, when the original project, the Chinese web page loaded from the Assert will appear garbled, the solution is to assign HTML encoding. As follows
[HTML] View plain copy
- <meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
- <script type= "Text/javascript" >
- function Javacalljs () {
- document.getElementById ("Content"). InnerHTML + =
- "<br\>java called the JS function";
- }
- function Javacalljswithargs (ARG) {
- document.getElementById ("Content"). InnerHTML + =
- ("<br\>" +arg);
- }
- </script>
- <body>
- This is my HTML <br/>
- <a onclick= "window.wst.startFunction ()" > Click Invoke Java Code </a><br/>
- <a onclick= "window.wst.startFunction (' Hello World ')" > Click Invoke Java code and pass parameters </a>
- <br/>
- <div id= "Content" > Display </div>
- </body>
The Java code is as follows
[Java] View plain copy
- Package Wst.webview;
- Import Android.annotation.SuppressLint;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.webkit.WebView;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- Import Android.widget.Toast;
- public class Mainactivity extends Activity {
- Private WebView Contentwebview = null;
- Private TextView Msgview = null;
- @SuppressLint ("setjavascriptenabled")
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Contentwebview = (WebView) Findviewbyid (R.id.webview);
- Msgview = (TextView) Findviewbyid (r.id.msg);
- Enable JavaScript
- Contentwebview.getsettings (). Setjavascriptenabled (True);
- Loading HTML from the assets directory
- Contentwebview.loadurl ("file:///android_asset/wst.html");
- Button button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (Btnclicklistener);
- Contentwebview.addjavascriptinterface (This, "WST");
- }
- Onclicklistener Btnclicklistener = new Button.onclicklistener () {
- public void OnClick (View v) {
- No parameter calls
- Contentwebview.loadurl ("Javascript:javacalljs ()");
- Passing parameter calls
- Contentwebview.loadurl ("Javascript:javacalljswithargs" ("+" ' Hello World ' "+") ");
- }
- };
- public void Startfunction () {
- Toast.maketext (This, "JS called the Java function", Toast.length_short). Show ();
- Runonuithread (New Runnable () {
- @Override
- public void Run () {
- Msgview.settext (Msgview.gettext () + "\njs called the Java function");
- }
- });
- }
- public void Startfunction (final String str) {
- Toast.maketext (this, str, toast.length_short). Show ();
- Runonuithread (New Runnable () {
- @Override
- public void Run () {
- Msgview.settext (Msgview.gettext () + "\njs called the Java function pass parameter:" + str);
- }
- });
- }
- }
layout file [HTML] view plain copy
- <?xml version= "1.0" encoding= "Utf-8"?>
- <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- android:orientation= "Vertical" >
- <webview
- Android:id= "@+id/webview"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- android:layout_weight= "9"/>
- <scrollview
- Android:id= "@+id/scrollview1"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content" >
- <textview
- Android:id= "@+id/msg"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- android:text= "Text"/>
- </ScrollView>
- <button
- Android:id= "@+id/button"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content"
- android:layout_weight= "1"
- Android:text= "Java Call js function"/>
- </LinearLayout>
I hope we have some help. resources
http://download.csdn.net/download/wangtingshuai/5106571
=============================================================================================================== =====
Concluding a few sentences:
1, WebView Load Web page, native code can interact with JS in the Web page and pass parameters (a parameter).
2, JS call native code that part of a sudden may not see very clear, here to tidy up:
?
12345678910111213141516171819202122 |
//native code方面
//使用addJavascriptInterface绑定“wst”
contentWebView.addJavascriptInterface(
this
,
"wst"
);
…
//添加需要响应的方法,注意必须为public
public
void
startFunction(
final String str) {
Toast.makeText(
this
,
"js调用了java函数"
, Toast.LENGTH_SHORT).show();
runOnUiThread(
new
Runnable() {
@Override
public
void
run() {
msgView.setText(msgView.getText() +
"\njs调用了java函数"
);
}
});
}
//js方面
<a onClick=
"window.wst.startFunction(‘hello world‘)" >点击调用java代码并传递参数</a><span><span style=
"line-height:20px;"
> </span></span>
|
Share to:
WebView JavaScript interacts with native code