一,安卓原生调用JS代码
1,js代码:
function handlePasteDataFromApp(pasteStr) {
showInfo('pasteData: aaaaa' + JSON.stringify(pasteStr).toString());
}
2,安卓代码
final Map<String,String> map = new HashMap<String,String>();
map.put("pasteData", clipboardContent);
final JSONObject json = new JSONObject(map);
webview.loadUrl("javascript:handlePasteDataFromApp('" + json + "')");
二,JS调用安卓代码
1,安卓代码
创建接口
public interface JavaScriptFunction {
/**
* 统计加入购物车行为
* @param json
*/
void logAddedToCartEvent(String json);
}
使用接口,接收js调用
webview.addJavascriptInterface(new JavaScriptFunction() {
@Override
@JavascriptInterface
public void logAddedToCartEvent(String json) {
// to do some
}
}, "JavaScriptFunction");
2,JS代码
var params = {
contentData: 'contentData',
contentId: contentId,
contentType: contentType,
currency: currency,
valueToSum: valueToSum
}
window.JavaScriptFunction.logAddedToCartEvent(JSON.stringify(params))