网页代码:
<html >
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<script>
function fillContent(){
document.getElementById("content").innerHTML =
"java调用javascript哈哈,这些话事javascript搞出来的";
}
</script>
<body>
<p><a onClick="window.demo.callPhone()" href="">打电话</a></p>
<p id="content"></p>
<p>java和javascript相互调用</p>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<script>
function fillContent(){
document.getElementById("content").innerHTML =
"java调用javascript哈哈,这些话事javascript搞出来的";
}
</script>
<body>
<p><a onClick="window.demo.callPhone()" href="">打电话</a></p>
<p id="content"></p>
<p>java和javascript相互调用</p>
</body>
</html>
1、javascript调用java代码
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//相当于得到一个浏览器
webview = (WebView) this.findViewById(R.id.webview);
//得到浏览器的设置
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.addJavascriptInterface(new Object(){
public void callPhone(){
System.out.println("callPhone()");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:5506"));
startActivity(intent);
}
}, "demo");
String url = this.getResources().getString(R.string.serverurl);
webview.loadUrl(url);
}
2、java调用javascript代码
public void callphone(View v){
webview.loadUrl("javascript:fillContent()");
}
webview.loadUrl("javascript:fillContent()");
}
完整代码: