首页 > 代码库 > H5调用Android拨打电话

H5调用Android拨打电话

技术分享

1.AndroidAndJSInterface.java

class AndroidAndJSInterface {
	/**
	 * 该方法将被js调用,用于加载数据
	 */
	@JavascriptInterface
	public void showcontacts() {
		// 下面的代码建议在子线程中调用
		String json = "[{\"name\":\"阿福\", \"phone\":\"18600012345\"}]";
		// 调用JS中的方法
		webView.loadUrl("javascript:show(‘" + json + "‘)");
	}

	/**
	 * 拨打电话
	 * @param phone
	 */
	@JavascriptInterface
	public void call(String phone) {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
		if (ActivityCompat.checkSelfPermission(JsCallJavaCallPhoneActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
			return;
		}
		startActivity(intent);

	}
}

2.deml.html

<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";
			td1.innerHTML = jsonobjs[y].name;
			td2.innerHTML = "<a href=‘javascript:Android.call(\""+ jsonobjs[y].phone+ "\")‘>"+ jsonobjs[y].phone+ "</a>";
		}
	} 
</script>
.............
<table border="0" width="100%" id="personTable" cellspacing="0">
	<tr>
		<td width="30%">姓名</td>
		<td align="center">电话</td>
	</tr>
</table>

 

H5调用Android拨打电话