Java和JavaScript通信

原文地址:Java和JavaScript通信 作者:androidrobot

Android设别具有很多强大的功能,如果可以用网页来展示实现,不是更好吗?

关键在于WebView类中的addJavaScriptInterface()方法,可以实现JavaScript调用Android程序中的方法,也可以实现Android调用JavaScript代码方法,需要做的就是用loadUrl()方法,将URL以javascript:function的形式传递给他。

具体例子如下:

/res/layout/main.xml

 


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0">
   
        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">
                android:text="TextView"
        android:id="@+id/textView1"
        android:textSize="24sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
       
                android:text="Call JavaScript From Android"
        android:id="@+id/button1"
        android:textSize="18sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
       
                android:text=""
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
       
   


//要展现的页面,内嵌套JS

/assets/index.html

 



 
  
 
 
  

WebView


  


  
  Display JavaScript alert
  

  


  


  
  
  Call Android from JavaScript
  

  


  


 

//源代码

/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 String TAG="LocalBrowser";
 //JavaScript调用进入专用于浏览器的线程,但是Android用户界面只能通过住UI线程实现
  //因此,Handler类进行转换
 private final Handler handler = new Handler();
 private WebView  webView;
 private TextView  textView;
 private Button  button;
 
    @Override
    public void onCreate(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,final String url,
           final String message,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 void onClick(View v) {
    // TODO Auto-generated method stub
    //调用定义在index.xml文件中的callJS()函数
    webView.loadUrl("javascript:callJS('hello from android')");
   }
  });
    }
    class AndroidBridge{
     public void callAndroid(final String  arg){
      handler.post(new Runnable() {
    
    public void run() {
     // TODO Auto-generated method stub
     Log.d(TAG, "callAndroid("+arg+")");
     textView.setText(arg);
  

你可能感兴趣的:(Java)