/**
* Android 端调用 WebView 中的 js 方法
*
* @
param function 方法名称
* @
param params 参数数组
*/
public static void letJavaScriptDo(final String function, final String... params) {
String[] jsParams = new String[params.length + 1];
jsParams[0] = function;
for (int i = 1; i < jsParams.length; i++) {
jsParams[i] = params[i - 1];
}
Message msg = new Message();
msg.obj = jsParams;
runJsHandler.sendMessage(msg);
}
private static Handler runJsHandler = new Handler() {
public void handleMessage(Message msg) {
String[] jsParams = (String[]) msg.obj;
String function = jsParams[0];
String jsUrl = "javascript:" + function + "(";
for (int i = 1; i < jsParams.length; i++) {
if (i != 1) {
jsUrl += ",";
}
jsUrl += "'" + jsParams[i].replace("'", "\\\'") + "'";
}
jsUrl += ");";
if (mWebView != null) {
mWebView.loadUrl(jsUrl);
}
}
};
暂时没发现问题,欢迎指教