Android和javascript相互调用2

package com.maiyu.jsdiaoyong;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 *
 *   @JavascriptInterface  必须添加js反射机制
 */
public class Main2Activity extends Activity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //加载页面
        webView = (WebView) findViewById(R.id.webView2);
        //允许JavaScript执行
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new MyWebViewClient());
        //找到Html文件,也可以用网络上的文件
        webView.loadUrl("file:/android_asset/index2.html");
        // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法
        webView.addJavascriptInterface(new Contact(), "contact");
    }

    private final class Contact {
        //JavaScript调用此方法拨打电话
        @JavascriptInterface
        public void call(String phone) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }

        //Html调用此方法传递数据
        @JavascriptInterface
        public void showcontacts() {
            //必须另开线程进行JS方法调用(否则无法调用)
            webView.post(new Runnable() {
                @Override
                public void run() {
                    String json = "[{\"name\":\"zxx\",\"amount\":\"9999999\",\"phone\":\"18600012345\"}]";
                    // 注意调用的JS方法名要对应上
                    // 调用JS中的方法
                    webView.loadUrl("javascript:show('" + json + "')");
                }
            });

        }
    }

    /**
     * webviewb必须实现的方法 ,解决
     */
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) {
                //打电话
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            } else if (url.startsWith("http:") || url.startsWith("https:")) {
                view.loadUrl(url);
            }
            return true;
        }

    }
}



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>Insert title here</title>
 <script type="text/javascript">
            function show(jsondata){
                    var jsonobjs = eval(jsondata);
                    var table = document.getElementById("personTable");
                    for(var y=0; y<jsonobjs.length; y++){
                        var tr = table.insertRow(table.rows.length);
                        var td1 = tr.insertCell(0);
                        var td2 = tr.insertCell(1);
                        td2.align = "center";
                        var td3 = tr.insertCell(2);
                        td3.align = "center";
                        td1.innerHTML = jsonobjs[y].name;
                        td2.innerHTML = jsonobjs[y].amount;
                        td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>";
                    }
            }
        </script>
</head>
<body οnlοad="javascript:contact.showcontacts()">
<table border="0" width="100%" id="personTable" cellspacing="0">
 <tr>
  <td width="30%">姓名</td>
  <td width="30%" align="center">存款</td>
  <td align="center">电话</td>
 </tr>
</table>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值