Android提供了一個很強大的WebView控件用來處理Web網頁,而在網頁中,JavaScript又是一個很舉足輕重的腳本。本文將介紹如何實現(xiàn)Java代碼和Javascript代碼的相互調用。
如何實現(xiàn)
實現(xiàn)Java和js交互十分便捷。通常只需要以下幾步。
1.WebView開啟JavaScript腳本執(zhí)行
2.WebView設置供JavaScript調用的交互接口。
3.客戶端和網頁端編寫調用對方的代碼。
本例代碼
為了便于講解,先貼出全部代碼
Java代碼
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);
}
}
}
前端網頁代碼
<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>
調用示例
js調用Java
調用格式為window.jsInterfaceName.methodName(parameterValues) 此例中我們使用的是control作為注入接口名稱。
function toastMessage(message) {
window.control.toastMessage(message)
}
function sumToJava(number1, number2){
window.control.onSumResult(number1 + number2)
}
Java調用JS
webView調用js的基本格式為webView.loadUrl(“javascript:methodName(parameterValues)”)
調用js無參無返回值函數(shù)
String call = "javascript:sayHello()";
webView.loadUrl(call);
調用js有參無返回值函數(shù)
注意對于字符串作為參數(shù)值需要進行轉義雙引號。
String call = "javascript:alertMessage(\"" + "content" + "\")";
webView.loadUrl(call);
調用js有參數(shù)有返回值的函數(shù)
Android在4.4之前并沒有提供直接調用js函數(shù)并獲取值的方法,所以在此之前,常用的思路是 java調用js方法,js方法執(zhí)行完畢,再次調用java代碼將值返回。
1.Java調用js代碼
String call = "javascript:sumToJava(1,2)";
webView.loadUrl(call);
2.js函數(shù)處理,并將結果通過調用java方法返回
function sumToJava(number1, number2){
window.control.onSumResult(number1 + number2)
}
3.Java在回調方法中獲取js函數(shù)返回值
@JavascriptInterface
public void onSumResult(int result) {
Log.i(LOGTAG, "onSumResult result=" + result);
}
4.4處理
Android 4.4之后使用evaluateJavascript即可。這里展示一個簡單的交互示例 具有返回值的js方法
function getGreetings() {
return 1;
}
java代碼時用evaluateJavascript方法調用
private void testEvaluateJavascript(WebView webView) {
webView.evaluateJavascript("getGreetings()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Log.i(LOGTAG, "onReceiveValue value=" + value);
}});
}
輸出結果
I/MainActivity( 1432): onReceiveValue value=1
注意
1.上面限定了結果返回結果為String,對于簡單的類型會嘗試轉換成字符串返回,對于復雜的數(shù)據(jù)類型,建議以字符串形式的json返回。
2.evaluateJavascript方法必須在UI線程(主線程)調用,因此onReceiveValue也執(zhí)行在主線程。
疑問解答
Alert無法彈出
你應該是沒有設置WebChromeClient,按照以下代碼設置
myWebView.setWebChromeClient(new WebChromeClient() {});
Uncaught ReferenceError: functionName is not defined
問題出現(xiàn)原因,網頁的js代碼沒有加載完成,就調用了js方法。解決方法是在網頁加載完成之后調用js方法
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//在這里執(zhí)行你想調用的js函數(shù)
}
});
Uncaught TypeError: Object [object Object] has no method
安全限制問題
如果只在4.2版本以上的機器出問題,那么就是系統(tǒng)處于安全限制的問題了。Android文檔這樣說的
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
中文大意為
警告:如果你的程序目標平臺是17或者是更高,你必須要在暴露給網頁可調用的方法(這個方法必須是公開的)加上@JavascriptInterface注釋。如果你不這樣做的話,在4.2以以后的平臺上,網頁無法訪問到你的方法。
解決方法
1.將targetSdkVersion設置成17或更高,引入@JavascriptInterface注釋
2.自己創(chuàng)建一個注釋接口名字為@JavascriptInterface,然后將其引入。注意這個接口不能混淆。這種方式不推薦,大概在4.4之后有問題。
注,創(chuàng)建@JavascriptInterface代碼
public @interface JavascriptInterface {
}
代碼混淆問題
如果在沒有混淆的版本運行正常,在混淆后的版本的代碼運行錯誤,并提示Uncaught TypeError: Object [object Object] has no method,那就是你沒有做混淆例外處理。 在混淆文件加入類似這樣的代碼
-keep class com.example.javajsinteractiondemo$JsInteration {
*;
}
All WebView methods must be called on the same thread
過濾日志曾發(fā)現(xiàn)過這個問題。
E/StrictMode( 1546): java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {528712d4} called on Looper (JavaBridge, tid 121) {52b6678c}, FYI main Looper is Looper (main, tid 1) {528712d4})
E/StrictMode( 1546): at android.webkit.WebView.checkThread(WebView.java:2063)
E/StrictMode( 1546): at android.webkit.WebView.loadUrl(WebView.java:794)
E/StrictMode( 1546): at com.xxx.xxxx.xxxx.xxxx.xxxxxxx$JavaScriptInterface.onCanGoBackResult(xxxx.java:96)
E/StrictMode( 1546): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
E/StrictMode( 1546): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
E/StrictMode( 1546): at android.os.Handler.dispatchMessage(Handler.java:102)
E/StrictMode( 1546): at android.os.Looper.loop(Looper.java:136)
E/StrictMode( 1546): at android.os.HandlerThread.run(HandlerThread.java:61)
在js調用后的Java回調線程并不是主線程。如打印日志可驗證
ThreadInfo=Thread[WebViewCoreThread,5,main]
解決上述的異常,將webview操作放在主線程中即可。
webView.post(new Runnable() {
@Override
public void run() {
webView.loadUrl(YOUR_URL).
}
});