Android模块化编程——WebView使用之与JavaScript交互

WebView调用JS:
   
   
   
   
可以这样:
mWebView.loadUrl("javascript:backClick()");
//获得手机的IMSI码(这是统计安装次数时写的,首次安装,发送IMSI码,详情请找我的其他博文)
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTelephonyMgr.getSubscriberId();
mWebView.loadUrl("javascript:androidInstallSTAT("+IMAI+")");
JS调用WebView:
a.JavaScript代码
    
    
    
    
$('#btn_sm').click(function () {
if (/android/i.test(navigator.userAgent)) { // android
window.XYLAndroidJSInterface.scanQRCode();
} else if (/ipad|iphone|mac/i.test(navigator.userAgent)) { // ios
document.location.href = "testios:SYS";
} else {
return;
}
});
b.Android代码(当然,你首先得  mWebView .getSettings().setJavaScriptEnabled(true);
    
    
    
    
mWebView.addJavascriptInterface(new Object() {
@JavascriptInterface
public void scanQRCode() {
//网页上调用后执行
Intent intent = new Intent(MainActivity.this, MipcaActivityCapture.class);
startActivity(intent);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, FILECHOOSER_RESULTCODE);
}
@JavascriptInterface
public void setGoodsCartSize(final int paramAnonymousInt) {
//
}
}, "XYLAndroidJSInterface");
其他交互方式:shouldOverrideUrlLoadint()方法可以捕获链接,执行特定操作。比如某页需要新开,某页需要清除历史,等等
    
    
    
    
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebChromeClient(new WebChromeClientImpl());
//这下边是调用的两个类。上下是两部分代码,请忽视我的格式,参悟我的思想
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {//每次打开新页面都执行
if (url.startsWith("tel:")) {//判断也在这里不太好,每次都需要,可以优化
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.goBack();//因为打开电话页面的时候,view是加载了tel:40012345页面。所以这里要返回一下。
// return true;
}else{
view.loadUrl(url);//如果不是tel的话,就用自身加载页面。如果不写这句,会跳到系统浏览器加载。
}
return true;
}
}
     
     
     
     
class WebChromeClientImpl extends WebChromeClient {
WebChromeClientImpl() {
}
//Alert,app执行  
public boolean onJsAlert(WebView paramWebView, String paramString1, String paramString2,
JsResult paramJsResult) {
AlertDialog.Builder localBuilder = new AlertDialog.Builder(paramWebView.getContext());
localBuilder.setTitle("*提示").setMessage(paramString2).setPositiveButton("确定", null);
localBuilder.setCancelable(false);
localBuilder.setIcon(R.drawable.icon_alpha);
localBuilder.create().show();
paramJsResult.confirm();
// Toast.makeText(MainActivity.this, "toastle",
// Toast.LENGTH_SHORT).show();
return true;
}
 
public void openFileChooser(ValueCallback<Uri> paramValueCallback) {
// android 3.0+
MainActivity.this.mUploadMessage = paramValueCallback;
Intent localIntent = new Intent("android.intent.action.GET_CONTENT");
localIntent.addCategory("android.intent.category.OPENABLE");
localIntent.setType("*/*");
MainActivity.this.startActivityForResult(Intent.createChooser(localIntent, "file Browser"), 1);
}
 
public void openFileChooser(ValueCallback<Uri> paramValueCallback, String paramString) {
// Toast.makeText(MainActivity.this, "22222",
// android > 4.1.1
MainActivity.this.mUploadMessage = paramValueCallback;
Intent localIntent = new Intent("android.intent.action.GET_CONTENT");
localIntent.addCategory("android.intent.category.OPENABLE");
localIntent.setType("*/*");
MainActivity.this.startActivityForResult(Intent.createChooser(localIntent, "File Chooser"), 1);
}
}
openFileChooser是有关图片上传的


你可能感兴趣的:(JavaScript,android,webView)