android中实现JavaScript与Java之间实现互相调用
2012-06-20 08:54
651 查看
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>
看看效果图吧!
下面有我整理出的源代码。
//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>
看看效果图吧!
下面有我整理出的源代码。
相关文章推荐
- iOS中Objective-C与JavaScript之间相互调用的实现(实现了与Android相同的机制)
- Android 实现WebView与JavaScript的互相调用
- iOS中Objective-C与JavaScript之间相互调用的实现(实现了与Android相同的机制)
- JsBridge实现Javascript和Java的互相调用
- Android中在WebView里实现Javascript调用Java类的方法
- JsBridge实现JavaScript和Android的互相调用
- Android中java和javascript方法的互相调用示例
- Android中WebView实现Javascript调用Java类方法
- 在android的webview中 javascript与java代码互相调用
- Android中实现WebView和JavaScript的互相调用详解
- Android开发,WebView下Java和JavaScript互相调用(2)
- Android下实现Java和JavaScript代码相互调用的代码
- 【转】JsBridge实现JavaScript和Java的互相调用
- JsBridge实现Javascript和Java的互相调用
- iOS中Objective-C与JavaScript之间相互调用的实现(实现了与Android相同的机制)
- 安卓学习笔记---JNI实现Java与C之间进行互相调用
- iOS中Objective-C与JavaScript之间相互调用的实现(实现了与Android相同的机制)转
- Android WebView中javascript和java的互相调用
- android中JavaScript与java接口互相调用产生的失效问题
- Android加载网页JavaScript与Java之间的相互调用