引用:http://blog.163.com/ws_wishao/blog/static/173046963201112195020124/

ERROR/InputDispatcher(61): channel '40723150 com.JavaScriptCom/com.JavaScriptCom.JavaScriptCom (server)' ~ Channel is unrecoverably broken and will be disposed!
这个错误,必须用2.1的模拟器,这个估计是BUG.

附代码:
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.JavaScriptCom" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".JavaScriptCom" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>

JavaScriptCom.java

package com.JavaScriptCom;
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 JavaScriptCom extends Activity {
final class runJavaScript {
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);
}

});
}
}

private EditText txt;
private WebView wv;
private Button btn;
private Handler h = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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");
String url = "file:///android_asset/android.html";
wv.loadUrl(url);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
wv.loadUrl("javascript:get4Android('"
+ txt.getText().toString() + "')");
}
});
}
}

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>

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" />

更多相关文章

  1. 修改Android 模拟器IMEI
  2. Android Service绑定的时候没有回调onBound函数
  3. (android图像处理)android之bitmap各种常用函数
  4. Android jni调用,实现自己的JNI_OnLoad函数
  5. android 模拟器不能上网问题
  6. Android中SensorManager.getRotationMatrix函数:计算出旋转矩阵,
  7. Android JNI(实现自己的JNI_OnLoad函数)

随机推荐

  1. Android使用NDK进行联网签名认证
  2. android主线程中Looper.loop()为什么不会
  3. AES Crypto 在 Android(安卓)P(Android(安
  4. android中SimpleCursorAdapter _id错误的
  5. android——ListView功能的实现
  6. 总结系列-Android的文件系统
  7. android 笔记handler
  8. Android(安卓)SDK下载和更新失败的解决方
  9. android 图片设置圆角
  10. Android(安卓)获取当前日期 时间