从javascript调用android函数
我想在html中创建一个按钮,并通过javascript从android中调用该按钮。我已经编写了以下代码,但不起作用: 我是android的初学者从javascript调用android函数,javascript,android,Javascript,Android,我想在html中创建一个按钮,并通过javascript从android中调用该按钮。我已经编写了以下代码,但不起作用: 我是android的初学者 public class MainActivity extends Activity { private static final String PIC_WIDTH = null; @Override protected void onCreate(Bundle savedInstanceState) {
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>
呼叫
$(“#makeCall”)。单击(函数(){
log(“内部点击javascript”);
log(window.android);
makeCall();
});
任何帮助???确保您的$(“#makeCall”)
方法以html响应
然后做以下更改。它会工作的
public class JsInterface{
@JavascriptInterface
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公开方法。确保您的$(“#makeCall”)
方法以html响应
然后做以下更改。它会工作的
public class JsInterface{
@JavascriptInterface
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公开方法。确保您的$(“#makeCall”)
方法以html响应
然后做以下更改。它会工作的
public class JsInterface{
@JavascriptInterface
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公开方法。确保您的$(“#makeCall”)
方法以html响应
然后做以下更改。它会工作的
public class JsInterface{
@JavascriptInterface
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。我回答自己的问题,以便它可以帮助其他人。:) 终于解决了:
myWebView.setWebViewClient(新的MyAppWebViewClient())代码>它丢失,因此javascript无法工作。我必须按照上面Hardik Trivedi的建议添加@JavascriptInterface
。
它现在运行良好。我在回答我自己的问题,以便它能帮助其他人。:)
终于解决了:
myWebView.setWebViewClient(新的MyAppWebViewClient())代码>它丢失,因此javascript无法工作。我必须按照上面Hardik Trivedi的建议添加@JavascriptInterface
。
它现在运行良好。我在回答我自己的问题,以便它能帮助其他人。:)
终于解决了:
myWebView.setWebViewClient(新的MyAppWebViewClient())代码>它丢失,因此javascript无法工作。我必须按照上面Hardik Trivedi的建议添加@JavascriptInterface
。
它现在运行良好。我在回答我自己的问题,以便它能帮助其他人。:)
终于解决了:
myWebView.setWebViewClient(新的MyAppWebViewClient())代码>它丢失,因此javascript无法工作。我必须按照上面Hardik Trivedi的建议添加@JavascriptInterface
。
现在工作正常。只需添加webView.getSettings().setJavaScriptEnabled(true);为我工作。
也必须添加@JavascriptInterface。只需添加webView.getSettings().setJavaScriptEnabled(true);为我工作。
也必须添加@JavascriptInterface。只需添加webView.getSettings().setJavaScriptEnabled(true);为我工作。
也必须添加@JavascriptInterface。只需添加webView.getSettings().setJavaScriptEnabled(true);为我工作。
还必须添加@JavascriptInterface。您应该看看Cordova和Sencha框架……什么是“不工作”的意思,您是否在控制台中收到错误,它是否在做任何事情,打印输出是否说它正在进行中,等等。当我点击按钮时,什么都没有发生。它说android不是defined@SSS请发布你的完整index.html,我已经在解决阶段。所以请发布完整的html页面,这个js calledI发布了完整的index.html。查看编辑后的代码您应该看看Cordova和Sencha框架…什么是“不工作”的意思,您是否在控制台中收到错误,它是否正在做任何事情,打印输出是否说它正在进行中,等等。当我单击按钮时,什么都没有发生。它说android不是defined@SSS请发布你的完整index.html,我已经在解决阶段。所以请发布完整的html页面,这个js calledI发布了完整的index.html。查看编辑后的代码您应该看看Cordova和Sencha框架…什么是“不工作”的意思,您是否在控制台中收到错误,它是否正在做任何事情,打印输出是否说它正在进行中,等等。当我单击按钮时,什么都没有发生。它说android不是defined@SSS请发布你的完整index.html,我已经在解决阶段。所以请发布完整的html页面,这个js calledI发布了完整的index.html。查看编辑后的代码您应该看看Cordova和Sencha框架…什么是“不工作”的意思,您是否在控制台中收到错误,它是否正在做任何事情,打印输出是否说它正在进行中,等等。当我单击按钮时,什么都没有发生。它说android不是defined@SSS请发布你的完整index.html,我已经在解决阶段。所以请发布完整的html页面,这个js calledI发布了完整的index.html。查看编辑的代码如果我这样做,它将给出错误:JavascriptInterface无法解析为type@JavascriptInterface是在果冻豆中引入的。您需要从EclipseIDE中将项目构建目标设置为API级别17,转到Project->Properties。选择Android并设置APK将在旧版本的Android上工作的目标;log(window.android);两行打印?它在内部打印。然后告诉window.android没有定义,据我所知,没有什么比window.android更好的了。请把它拿走?你写这行的目的是什么?如果我这样做,它将给出错误:JavascriptInterface无法解析为type@JavascriptInterface是在果冻豆中引入的。您需要从EclipseIDE中将项目构建目标设置为API级别17,转到Project->Properties。选择Android并设置APK将在旧版本的Android上工作的目标;log(window.android);两行打印?它在内部打印。然后告诉window.android没有定义,据我所知,没有什么比window.android更好的了。请把它拿走?你写那句话的目的是什么?如果我那样做,那就错了