webview提供了接口让javascript访问Java应用程序,WebView.addJavascriptInterface()方法就是实现交互接口的好东西呀!当然也是很危险的!看下面例子:

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send To JavaScript"
/>
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<WebView
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

//WebJavaScript.java

package com.hl;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WebJavaScript extends Activity {
private EditText txt;
private WebView wv;
private Button btn;
private Handler h = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (EditText) findViewById(R.id.txt);
wv = (WebView) findViewById(R.id.wv);
btn = (Button) findViewById(R.id.btn);

WebSettings webSettings = wv.getSettings();

webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(false);
webSettings.setSavePassword(false);
webSettings.setSupportZoom(false);

wv.addJavascriptInterface(new runJavaScript(), "myjs");
//myjs是自己定义的,供javascript访问的接口

String url = "file:///android_asset/android.html";
wv.loadUrl(url);

btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//调用javascript的函数get4Android(str)
wv.loadUrl("javascript:get4Android('"+ txt.getText().toString() + "')");
}
});
}
//The Java object that is bound runs in another thread and not in the thread that it was constructed in.文档的一句话!
final class runJavaScript{//这个Java 对象是绑定在另一个线程里的,
public void runOnAndroidJavaScript(final String str){
h.post(new Runnable(){
@Override
public void run() {//这里应该特别注意的
TextView show = (TextView) findViewById(R.id.show);
show.setText("This is a message from javascript:"+str);
}

});
}
}
}

//放在assets文件夹的html文件,android.html

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript">
function get4Android(str){
document.getElementById("show").innerHTML="This is a message from android:"+str;
}
function send2Android(){
var str = document.getElementById("mess").value;
window.myjs.runOnAndroidJavaScript(str);//调用android的函数
}
</script>
</head>

<body>
<input type="text" id="mess" />
<input type="button" value="Send To Android" onclick="send2Android()"/>
<div id="show"></div>
</body>
</html>
看看效果图吧!

下面有我整理出的源代码。

更多相关文章

  1. Android(安卓)调用已安装市场,去应用市场评分
  2. android2.3选择相册图片或者调用系统照相
  3. 详解Android(安卓)9.0 私有API禁用机制
  4. Android之Service 的生命周期
  5. Android短彩信收发流程(Framework)
  6. Android(安卓)ViewManager实例
  7. Android(安卓)TV的音量键实现流程
  8. 写在20120524:aidl
  9. [Android中react-native调用Native]

随机推荐

  1. android中shape
  2. Android RadioGroup和RadioButton使用
  3. 【Android Training视频系列】第4讲Build
  4. Android所有系统资源图标android.R.drawa
  5. Intent实现Android间的页面跳转
  6. android 开发BUG
  7. Android中事件分发机制分析
  8. Android下基于XML的Graphics shape使用方
  9. 【Android学习入门】Android studio基本
  10. dev android project from cmd