热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android与javascript交互(BindingJavaScriptcodetoAndroidcode)

1.如何建立android与javascript的交互TobindanewinterfacebetweenyourJavaScriptandAndroidcode,

1.如何建立android与Javascript的交互

To bind a new interface between your Javascript and Android code, call addJavascriptInterface(), passing it a class instance to bind to your Javascript and an interface name that your Javascript can call to access the class.
For example:

public class WebActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().hide();
//加载页面
webView = (WebView) findViewById(R.id.myweb);
//允许Javascript执行
webView.getSettings().setJavascriptEnabled(true);
//找到Html文件,也可以用网络上的文件
webView.loadUrl("file:///android_asset/index.html");
// 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法
webView.addJavascriptInterface(new Contact(), "contact");
//让网页可以弹出alert、confrim对话框
webView.setWebChromeClient(new WebChromeClient() {});
}

private final class Contact {

//Javascript调用此方法拨打电话
@JavascriptInterface
public void call(String phone) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone)));
}
@JavascriptInterface
public String getVersion()
{
String versiOnName= "";
try {
PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
versiOnName= pi.versionName;
return versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
//Html调用此方法传递数据
@JavascriptInterface
public void showcontacts() {
System.out.println("---------showcontact---------");
final String json = "[{\"name\":\"zxx\", \"amount\":\"9999999\", \"phone\":\"18600012345\"}]";
// 调用JS中的方法
webView.post(new Runnable() {
@Override
public void run() {
// webView.loadUrl("Javascript: alert(" + data +")");
webView.loadUrl("Javascript:show('" + json + "')");
}
});
}
}
}

html页面代码


<html>
<head lang="en">
<meta charset="UTF-8">
<title>Insert title heretitle>
<script type="text/Javascript">
function show(jsondata){
var jsOnobjs= eval(jsondata);
alert(jsonobjs[0].name);
jsOnobjs= [{"name":"zxx", "amount":"9999999", "phone":"18600012345"}];
var table = document.getElementById("personTable");
for(var y=0; y var tr = table.insertRow(table.rows.length);
var td1 = tr.insertCell(0);
var td2 = tr.insertCell(1);
td2.align = "center";
td2.Onclick= showVersion;
var td3 = tr.insertCell(2);
td3.align = "center";
td1.innerHTML = jsonobjs[y].name;
td2.innerHTML = jsonobjs[y].amount;
td3.innerHTML = ""+ jsonobjs[y].phone+ ""
;
}
}
function showVersion()
{

alert(contact.getVersion());
}
script>
head>
<body onload="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>

2.出现的一些问题

(1).W/WebView(2088): java.lang.Throwable: A WebView method was called on thread ‘JavaBridge’. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {b3dbcb18} called on Looper (JavaBridge, tid 120) {b44a1af8}, FYI main Looper is Looper (main, tid 1) {b3dbcb18})

public void showcontacts() {
System.out.println("---------showcontact---------");
final String json = "[{\"name\":\"zxx\", \"amount\":\"9999999\", \"phone\":\"18600012345\"}]";
// 调用JS中的方法
webView.loadUrl("Javascript:show('" + json + "')");
}

该方法如果这样直接调用Javascript方法就会报上面错误。
(2).Uncaught TypeError: Object [object Object] has no method
下面是一些官方说明
Caution: If you’ve set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your Javascript (the method must also be public). If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.
(3).Uncaught ReferenceError: functionName is not defined
问题出现原因,网页的js代码没有加载完成,就调用了js方法。解决方法是在网页加载完成之后调用js方法。

myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//在这里执行你想调用的js函数
}
});

推荐阅读
author-avatar
色系迷人_777
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有