【发布时间】:2014-01-10 09:15:41
【问题描述】:
我想在 html 中创建一个按钮,并通过 javascript 从 android 的该按钮进行调用。我编写了以下代码,但它不起作用: 我是安卓初学者。
public class MainActivity extends Activity {
private static final String PIC_WIDTH = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(new JsInterface(), "android");
myWebView.loadUrl("file:///android_asset/www/index.html");
}
public class JsInterface{
public void makeCall()
{
Log.i("Myactivity","inside android makecall");
// Here call any of the public activity methods....
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9611396958"));
startActivity(callIntent);
}
}
}
在javascript中:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body class="bgClass" id="body" ontouchstart="">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script src="js/myScript.js"></script>
<button id="makeCall" >CALL</button>
<script>
$("#makeCall").click(function(){
console.log("inside click javascript");
console.log(window.android);
android.makeCall();
});
</script>
</body>
</html>
有什么帮助吗??
【问题讨论】:
-
你应该看看 Cordova 和 Sencha 框架...
-
“不工作”是什么意思,您是否在控制台中收到错误,是否正在执行任何操作,是否打印出说它已经到了这么远等等。
-
当我点击按钮时没有任何反应。它说android没有定义
-
@SSS 请发布您的完整 index.html ,我已经处于解决阶段。所以请发布这个js调用的完整html页面
-
我发布了完整的 index.html。查看修改后的代码
标签: javascript android