Android设别具有很多强大的功能,如果可以用网页来展示实现,不是更好吗?
关键在于WebView类中的addJavaScriptInterface()方法,可以实现JavaScript调用Android程序中的方法,也可以实现Android调用JavaScript代码方法,需要做的就是用loadUrl()方法,将URL以javascript:function的形式传递给他。
具体例子如下:
/res/layout/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"
>
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0">
</WebView>
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/linearLayout1"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:padding="5sp">
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:textSize="24sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<Button
android:text="Call JavaScript From Android"
android:id="@+id/button1"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<TextView
android:text=""
android:id="@+id/textView2"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
//要展现的页面,内嵌套JS
/assets/index.html
<?xml version="1.0"encoding="utf-8"?>
<html>
<head>
<scriptlanguage="javascript">
function callJS(arg){
document.getElementByIdx_x_x('replaceme').innerHTML=arg;
}
</script>
</head>
<body>
<h1>WebView</h1>
<p>
<a href="#"onclick="window.alert('Alert FromJavascript')">
Display JavaScript alert
</a>
</p>
<p>
<!--鏅�氭祻瑙堝櫒鏄棤娉曡皟鐢ㄨ繖涓柟娉曠殑锛屾墍浠ュ簲璇ラ噸瀹氫箟杩欎釜瀵硅薄 -->
<a href="#"onclick="window.android.callAndroid('Hello fromBrowser')">
Call Android fromJavaScript
</a>
</p>
<pid="replaceme"></p>
</body>
</html>
//源代码
/src/LocalBrowserActivity.java
package com.liwei;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.JsResult;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class LocalBrowserActivity extends Activity {
private static final StringTAG="LocalBrowser";
//JavaScript调用进入专用于浏览器的线程,但是Android用户界面只能通过住UI线程实现
//因此,Handler类进行转换
private final Handler handler = newHandler();
private WebView webView;
private TextView textView;
private Button button;
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView)this.findViewById(R.id.webView1);
textView = (TextView)this.findViewById(R.id.textView2);
button =(Button)this.findViewById(R.id.button1);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new AndroidBridge(),"android");
webView.setWebChromeClient(new WebChromeClient(){
public boolean onJsAlert(final WebView view,finalString url,
final Stringmessage,JsResult ss){
Toast.makeText(LocalBrowserActivity.this,message, 3000).show();
ss.confirm();
return true;
}
});
//对于Android浏览器来说,file:///android_assets/文件名形式的UrL具有特殊的意义。
//注意有三个(///)
webView.loadUrl("file:///android_asset/index.html");
button.setOnClickListener(new OnClickListener() {
public voidonClick(View v) {
//TODO Auto-generated method stub
//调用定义在index.xml文件中的callJS()函数
webView.loadUrl("javascript:callJS('hellofrom android')");
}
});
}
classAndroidBridge{
public void callAndroid(finalString arg){
handler.post(new Runnable(){
publicvoid run() {
//TODO Auto-generated method stub
Log.d(TAG,"callAndroid("+arg+")");