Android提供了一個很強大的WebView控件用來處理Web網(wǎng)頁,而在網(wǎng)頁中,JavaScript又是一個很舉足輕重的腳本。本文將介紹如何實現(xiàn)Java代碼和Javascript代碼的相互調(diào)用。
如何實現(xiàn)
實現(xiàn)Java和js交互十分便捷。通常只需要以下幾步。
- WebView開啟JavaScript腳本執(zhí)行
- WebView設(shè)置供JavaScript調(diào)用的交互接口。
- 客戶端和網(wǎng)頁端編寫調(diào)用對方的代碼。
本例代碼
為了便于講解,先貼出全部代碼
Java代碼
lineos:false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package com.example.javajsinteractiondemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String LOGTAG = "MainActivity";
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new JsInteration(), "control");
myWebView.setWebChromeClient(new WebChromeClient() {});
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
testMethod(myWebView);
}
});
myWebView.loadUrl("file:///android_asset/js_java_interaction.html");
}
private void testMethod(WebView webView) {
String call = "javascript:sayHello()";
call = "javascript:alertMessage(\"" + "content" + "\")";
call = "javascript:toastMessage(\"" + "content" + "\")";
call = "javascript:sumToJava(1,2)";
webView.loadUrl(call);
}
public class JsInteration {
@JavascriptInterface
public void toastMessage(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
@JavascriptInterface
public void onSumResult(int result) {
Log.i(LOGTAG, "onSumResult result=" + result);
}
}
}
|
前端網(wǎng)頁代碼
lineos:false js_java_interaction.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<html>
<script type="text/javascript">
function sayHello() {
alert("Hello")
}
function alertMessage(message) {
alert(message)
}
function toastMessage(message) {
window.control.toastMessage(message)
}
function sumToJava(number1, number2){
window.control.onSumResult(number1 + number2)
}
</script>
Java-Javascript Interaction In Android
</html>
|
調(diào)用示例
js調(diào)用Java
調(diào)用格式為window.jsInterfaceName.methodName(parameterValues) 此例中我們使用的是control作為注入接口名稱。
lineos:false
1
2
3
4
5
6
7
|
function toastMessage(message) {
window.control.toastMessage(message)
}
function sumToJava(number1, number2){
window.control.onSumResult(number1 + number2)
}
|
Java調(diào)用JS
webView調(diào)用js的基本格式為webView.loadUrl(“javascript:methodName(parameterValues)”)
調(diào)用js無參無返回值函數(shù)
lineos:false
1
2
|
String call = "javascript:sayHello()";
webView.loadUrl(call);
|
調(diào)用js有參無返回值函數(shù)
注意對于字符串作為參數(shù)值需要進行轉(zhuǎn)義雙引號。
lineos:false
1
2
|
String call = "javascript:alertMessage(\""
|
該文章在 2014/11/24 21:52:29 編輯過
|
Copyright 2010-2025
ClickSun All Rights Reserved