android 中Java和javascript的交互
2011-03-20 23:56
381 查看
webview提供了接口让javascript访问Java应用程序,WebView.addJavascriptInterface()方法就是实现交互接口的好东西呀!当然也是很危险的!看下面例子:
main.xml
WebJavaScript.java
放在assets文件夹的html文件,android.html
![](http://hi.csdn.net/attachment/201103/20/0_1300636575nf2o.gif)
http://www.ways2u.com/?post=201
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" /> <mce: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的函数 } // --></mce:script> </head> <body> <input type="text" id="mess" /> <input type="button" value="Send To Android" onclick="send2Android()"/> <div id="show"></div> </body> </html>
![](http://hi.csdn.net/attachment/201103/20/0_1300636575nf2o.gif)
http://www.ways2u.com/?post=201
相关文章推荐
- Android 中利用WebViewJavascriptBridge 实现js和java的交互
- Android 利用WebViewJavascriptBridge 实现js和java的交互(一)
- Android Webview Java和Javascript安全交互
- Android中Java代码与JavaScript交互
- Android中Java和JavaScript交互实例
- Android 利用WebViewJavascriptBridge 实现js和java的交互(一)
- Android WebView Java和JavaScript的交互
- Android Java和JavaScript交互
- Android 利用WebViewJavascriptBridge 实现js和java的交互
- 【Android】android 中Java和javascript的交互
- Android WebView上实现JavaScript与Java交互
- android中WebView的Java与JavaScript交互
- Android 利用WebViewJavascriptBridge 实现js和java的交互(一),androidjsbridge
- Android中Java和JavaScript交互
- Android 利用WebViewJavascriptBridge 实现js和java的交互(一)
- Android中Java和JavaScript交互
- Android中Java与JavaScript交互的几种方式
- Android中Java和JavaScript交互代码示例3以及常见问题
- Android中Java和JavaScript交互实例
- Android 利用WebViewJavascriptBridge 实现js和java的交互