Android中WebView可以和JavaScript之间进行通信,这样Native代码就能很好的和本地或者远程服务器上的Html进行交互。写了一个最简单的例子,WebView和本地的Html代码进行交互。
webview.xml 先加入一个全频的webview控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
myname.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function myinterface()
{
document.getElementById("myname").innerHTML=window.myobj.getName(); //调用js中的方法
}
function callingAndroid(string)
{
myobj.setHi(string);
document.getElementById("myspeak").innerHTML=window.myobj.getSpeak();
}
</script>
</head >
<body οnlοad="myinterface()">
myName:
<div id="myname"></div>
<div id="myspeak"></div>
<input type="button" value="hello" onClick="callingAndroid('Hi')" />
</body>
</html>
webviewActivity.xml
package com.example.gsmcelllocation;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class webviewActivity extends Activity {
private WebView webView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
return super.onJsAlert(view, url, message, result);
}
});
webView.getSettings().setDefaultTextEncodingName("gbk");
webView.addJavascriptInterface(new myinterface() , "myobj");
webView.loadUrl("file:///android_asset/myname.html");
}
class myinterface{
String Speak = "***";
public myinterface() {
}
public String getName()
{
return "飞跃";
}
public void setHi(String str)
{
Speak = str;
}
public String getSpeak()
{
return Speak;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
webView.getSettings().setJavaScriptEnabled(true);
是否允许在webview中执行JavaScript代码,这里设置为true。
webView.addJavascriptInterface(new myinterface(this) , "myobj");
绑定Java对象到JavaScript中,这样就能在JavaScript中调用Java对象,实现通信。方法中第一个参数就是Java对象,第二个参数表示该Java对象的别名,在JavaScript中使用。
webView.loadUrl("file:///android_asset/myname.html");
WebView加载本地html代码,注意本地的html代码必须放在unity3d工程的/plugins/android/assets目录下(如果是纯android工程就放到/assets目录下),然后通过file:///android_asset/demo.html访问。