webview与html的交互:
基本设置:
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true); // 设置支持JavaScript脚本
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);// 设置允许访问文件数据
settings.setBuiltInZoomControls(false);// 设置支持缩放
settings.setSavePassword(false); // 设置是否保存密码
settings.setDefaultFontSize(16);
1.定义一个类 class Android
2.创建Android实体并传给webview
Android android = new Android();
mWebView.addJavascriptInterface(mandroid, "android");
3.html获取实体
js文件中代码如下
function getDriverInfo() { var appVersion = navigator.appVersion; if (appVersion.indexOf('Mac OS', 0) > 0) { return "ios"; } else if (appVersion.indexOf('Android', 0) > 0) { return "android"; } }
if (typeof NewPhone === "undefined") { var NewPhone = {}; var appVersion = getDriverInfo(); NewPhone.isFuctionData = function (json) { if (appVersion == "ios") { ios.isFuctionData(json); } else if (appVersion == "android") { android.isFuctionData(json); } } }if (typeof android === "undefined") { var android = {}; var w3navigation = new Array(); //鍥炶皟鍑芥暟鐨勬爤 var navbar = {}; var modalView = {}; android.presentModalView = function (url, callback) { window.javatojs.showVehicleQuery(url, callback); } android.presentDatePicker = function (defaultDate, callback) { window.javatojs.showDatePicker(defaultDate, callback); } android.cacheData = function (data, menuid) { window.javatojs.cacheData(data, menuid); } android.cacheDataComRisk = function (data1, data2, menuid1, menuid2) { window.javatojs.cacheDataComRisk(data1, data2, menuid1, menuid2); } android.restoreData = function (menuid, callback) { window.javatojs.restoreData(menuid, callback); } android.restoreDataComRisk = function (menuid1, menuid2, callback) { window.javatojs.restoreDataComRisk(menuid1, menuid2, callback); } android.requestPost = function (url, paras, succeed, fail) { window.javatojs.requestPost(url, paras, succeed, fail); } android.requestGet = function (url, succeed, fail) { window.javatojs.requestGet(url, succeed, fail); } android.sendData = function (url, actionType, key, data, success, fail) { window.javatojs.sendData(url, actionType, key, data, success, fail); } }