从Android调用JavaScript [英] Calling JavaScript from Android
问题描述
我无法从Android内部调用基本的JavaScript函数.当我从本教程克隆存储库时,然后将其剥离下来,我就能使代码正常运行.但是,当尝试创建自己的新项目时,我没有成功.
我引用了以下帖子,并且似乎在做同样的事情,但无济于事.两种方法之间没有明显的区别,所以我觉得这些文件之外可能还缺少依赖项?
Android在WebView中调用JavaScript函数
我想知道我是否在某个地方缺少细微的东西.
基本上,我只是想通过调用在其自己的文件中定义的JavaScript函数,将任何输出输出到控制台.项目结构如下:
主要-资产-index.html-sketch.js-Java-com.mypackage-MainActivity.java
我有一个文件 sketch.js
,其中具有以下功能
function hello(){console.log("hello world");}
还有一个带有以下代码的 index.html
文件
<!DOCTYPE html>< html>< head>< script src ="sketch.js" type ="text/javascript"></script></head><身体>< canvas></canvas></body></html>
然后从我的活动课中,我有以下Java代码
webView =(WebView)findViewById(R.id.webView);webView.setWebChromeClient(new WebChromeClient());webView.getSettings().setJavaScriptEnabled(true);webView.loadUrl("file:///android_asset/index.html");webView.evaluateJavascript("javascript:hello();",null);webView.evaluateJavascript("console.log('Hello world 2');",null);
在Android Studio的控制台中,我看到
[INFO:CONSOLE(1)]未捕获的ReferenceError:未定义hello",来源:(1)I/铬:[INFO:CONSOLE(1)]"Hello world 2",来源:(1)
如何解决此代码,以便能够调用位于单独文件中的简单JavaScript函数 hello()
?
根据CW answer ,
您需要等到页面加载完毕
private void helloJs(){如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.KITKAT){webView.evaluateJavascript("javascript:hello();",null);} 别的 {webView.loadUrl("javascript:hello();");}}webView.setWebViewClient(new WebViewClient(){public void onPageFinished(WebView view,String url){helloJs();}});
I am having trouble calling basic JavaScript functions from within Android. When I cloned the repository from this tutorial, and then stripped it down I was able to get the code to function correctly. However, when trying to create my own fresh project I have been unsuccessful.
I have referenced the following posts and appear to be doing the same, but to no avail. There is no noticeable different between the approaches, so I feel like there may be dependencies outside of these files that I am missing?
Android Calling JavaScript functions in WebView
Run javascript code in Webview
I'm wondering if I am missing something subtle somewhere.
Basically I am just trying to get any output to the console by calling a JavaScript function that is defined in its own file. The project structure is as follows:
main
- assets
- index.html
- sketch.js
- java
- com.mypackage
- MainActivity.java
I have a file sketch.js
with the following function inside
function hello() {
console.log("hello world");
}
And an index.html
file with the following code
<!DOCTYPE html>
<html>
<head>
<script src="sketch.js" type="text/javascript"></script>
</head>
<body>
<canvas></canvas>
</body>
</html>
Then from my activity class I have the following Java code
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/index.html");
webView.evaluateJavascript("javascript:hello();", null);
webView.evaluateJavascript("console.log('Hello world 2');", null);
In the console in Android Studio I see
[INFO:CONSOLE(1)] "Uncaught ReferenceError: hello is not defined", source: (1)
I/chromium: [INFO:CONSOLE(1)] "Hello world 2", source: (1)
How can I fix this code to be able to call the simple JavaScript function hello()
that is in a separate file?
According to CW answer,
You need to wait until your page is loaded
private void helloJs(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:hello();", null);
} else {
webView.loadUrl("javascript:hello();");
}
}
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
helloJs();
}
});
这篇关于从Android调用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!