android与原生的JS交互

package com.ada56.apps.taxi.ui.login;

 

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.webkit.JsResult;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Toast;

 

import com.ada56.apps.taxi.passenger.R;

 

public class JSAndroidActivity extends Activity {

    private WebView myWebView = null;

    @SuppressLint("SetJavaScriptEnabled")

    @Override

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_jsdemo);

        myWebView = (WebView) findViewById(R.id.myWebView);

        WebSettings webSettings = myWebView.getSettings();// 得到设置属性的对象

        webSettings.setJavaScriptEnabled(true);// 使能JavaScript

        webSettings.setDefaultTextEncodingName("GBK");//支持中文,否则页面中中文显示乱码

        // 限制在WebView中打开网页,而不用默认浏览器

        myWebView.setWebViewClient(new WebViewClient());

 

        // 如果不设置这个,JS代码中的按钮会显示,但是按下去却不弹出对话框

        // Sets the chrome handler. This is an implementation of WebChromeClient

        // for use in handling JavaScript dialogs, favicons, titles, and the

        // progress. This will replace the current handler.

        myWebView.setWebChromeClient(new WebChromeClient() {

            @Override

            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {

                return super.onJsAlert(view, url, message, result);

            }

 

        });

 

        // 用JavaScript调用Android函数:

        // 先建立桥梁类,将要调用的Android代码写入桥梁类的public函数

        // 绑定桥梁类和WebView中运行的JavaScript代码

        // 将一个对象起一个别名传入,在JS代码中用这个别名代替这个对象,可以调用这个对象的一些方法

        //myWebView.addJavascriptInterface(new WebAppInterface(this), "myInterfaceName");

        myWebView.addJavascriptInterface(new WebViewNative(this), "Native");

 

        //myWebView.loadUrl("file:///android_asset/sample.html");// 载入页面:本地html资源文件

        myWebView.loadUrl("http://192.168.211.61:8080/taxi/jsAndroid.html");

    }

    

    public void callJSMethod(View view){

    //用Android代码调用JavaScript函数:

        myWebView.loadUrl("javascript:myFunction('<>')");

        //这里实现的效果和在网页中点击第一个按钮的效果一致

    }

 

    /**

     * 自定义的Android代码和JavaScript代码之间的桥梁类

     */

    public class WebViewNative {

        Context mContext;

        /** Instantiate the interface and set the context */

        WebViewNative(Context c) {

            mContext = c;

        }

        /** Show a toast from the web page */

        public void showToast(String toast) {

            Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();

        }

    }

 

}

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/myRelativeLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

 

   

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:padding="40dp"

        android:text="@string/hello_world"

        tools:context=".WebJSActivity" />

 

   

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/textView1"

        android:onClick="callJSMethod"

        android:text="android_Button" />

 

   

        android:id="@+id/myWebView"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_below="@id/textView1" />

 

 

===========================================================================================================================

    

   

       

        JavaScript函数调用

       

       
 

       

       

       

       

   

    

   

       

        用JavaScript按钮调用Android代码
 

       

            value="Say hello" onClick="showAndroidToast('Hello Android!')" />

   

 

    Google

   

 

你可能感兴趣的:(android与原生的JS交互)