您的位置:首页 > 移动开发 > Android开发

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>

看看效果图吧!







下面有我整理出的源代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: