android webview js与android互动注意事项

用2.3.1开发会出 
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"  οnclick="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" />
    <WebView android:id="@+id/wv" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值