WebView myWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true);
首先 需要在Android程序中建立接口
final class InJavaScript { public void runOnAndroidJavaScript(final String str) { handler.post(new Runnable() { public void run() { TextView show = (TextView) findViewById(R.id.textview); show.setText(str); } }); } }//把本类的一个实例添加到js的全局对象window中,
webView.addJavascriptInterface(new InJavaScript(), "injs");在JavaScript中调用
function sendToAndroid(){ var str = "Cookie call the Android method from js"; window.injs.runOnAndroidJavaScript(str);//调用android的函数 }2 在Android中调用JS的方法
function getFromAndroid(str){ document.getElementById("android").innerHTML=str; }在Android调用该方法
Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { //调用javascript中的方法 webView.loadUrl("javascript:getFromAndroid('Cookie call the js function from Android')"); } });3 Android中处理JS的警告,对话框等
webView.setWebChromeClient(new WebChromeClient(){ //处理javascript中的alert public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { //构建一个Builder来显示网页中的对话框 Builder builder = new Builder(MainActivity.this); builder.setTitle("Alert"); builder.setMessage(message); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); builder.setCancelable(false); builder.create(); builder.show(); return true; }; //处理javascript中的confirm public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { Builder builder = new Builder(MainActivity.this); builder.setTitle("confirm"); builder.setMessage(message); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); builder.setCancelable(false); builder.create(); builder.show(); return true; }; @Override //设置网页加载的进度条 public void onProgressChanged(WebView view, int newProgress) { MainActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100); super.onProgressChanged(view, newProgress); } //设置应用程序的标题title public void onReceivedTitle(WebView view, String title) { MainActivity.this.setTitle(title); super.onReceivedTitle(view, title); } });Android中的调试,通过JS代码输出log信息
Js代码: console.log("Hello World"); Log信息: Console: Hello World http://www.example.com/hello.html :67在WebChromeClient中实现onConsoleMesaage()回调方法,让其在LogCat中打印信息
WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.setWebChromeClient(new WebChromeClient() { public void onConsoleMessage(String message, int lineNumber, String sourceID) { Log.d("MyApplication", message + " -- From line " + lineNumber + " of " + sourceID); } });以及:
WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.setWebChromeClient(new WebChromeClient() { public boolean onConsoleMessage(ConsoleMessage cm) { Log.d("MyApplication", cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId() ); return true; } });