js与android webview交互

时间:2023-03-08 19:35:20

0x01 js调用java代码

android webview中支持通过添加js接口

webview.addJavascriptInterface(new JsInteration(), "control");

参数说明:

第一个:java对象对应这个WebView的JavaScript上下文

第二个:调用java对象的js中引用对象

Parameters:
 1 object the Java object to inject into this WebView's JavaScript context. Null values are ignored.
 2 name the name used to expose the object in JavaScript

0x02 java调用js代码

构造一个可执行的js脚本字符串

webview.loadurl("js脚本");

0x03 关于type="file" input控件在android平台上没触发文件选择事件,android系统屏蔽其消息,需要设置

id_webview.setWebChromeClient(new WebChromeClient() {

			// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
XQSInspectionActivity.this.startActivityForResult(
Intent.createChooser(i, "ÎļþÑ¡Ôñ"),
XQSInspectionActivity.FILECHOOSER_RESULTCODE);
} // For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
} // For Android > 4.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
openFileChooser(uploadMsg, "");
}
});