Android webapp 中调用 JS 的原生代码

发布于 2025-01-03 18:19:05 字数 338 浏览 1 评论 0原文

我正在编写一个Android“网络”应用程序(我不会将应用程序上传到网络,而是在应用程序资源中设置HTML + JS)。这意味着,GUI 将是 HTML5,我将使用另一个“本机”线程从麦克风读取数据,并希望将“解析后的文本”发送到 HTML5/JS 代码。

这可能吗?在 Android 代码中,我看到如何从 JS 调用本机代码(我想要相反的)。

http://developer.android.com/guide/webapps/webview.html

I am writing an Android "web" app (I will not upload the APP to the web, but set the HTML+JS inside the application resources). That means, the GUI will be HTML5, and I will use another "native" thread to read from the microphone and hopefully send the "parsed text" to the HTML5/JS code.

Is this possible? In the Android code, I see how can I call native code from JS (and I want the opposite).

http://developer.android.com/guide/webapps/webview.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-10 18:19:05

是的,Android 在这方面确实很出色。

我正在发布来自我制作的随机示例应用程序的示例代码,但这应该可以帮助您继续。

首先,假设您必须为 WebView 控制类添加全局变量:

String name = "John";
String lastName = "Doe";

在控制 WebView 的 Android 类中(在 onCreate() 期间或设置 WebView 时):

webView.addJavascriptInterface(new JavaScriptInterface(), "android")

WebView 控制类的内部类:

/**
* Sets up the interface for getting access to Latitude and Longitude data
* from device
**/
private class JavaScriptInterface {
    public String getName() {
        return name;
    }
    public String getLastName() {
        return lastName;
    }
    public void onJavascriptButtonPushed(String text) {
        Toast.makeText(WebViewControllingClass.this, "The text of the pressed button is: "+text,Toast.LENGTH_SHORT).show();
    }
}

Android 端就是这样,现在通过 Javascript 使用它:

通过 JavaScript 获取名字和姓氏:

if (window.android){
    name = window.android.getName();
    lastName = window.android.getLastName();
}

如果你想举起吐司,设置一个 javascript,其功能为:

if (window.android){
    window.android.onJavascriptButtonPushed("Hello");
}

编辑:

我还没有尝试过这个,但是搜索产生了这个,尝试一下,它应该可以工作。

如果你想用在另一个方向:

JS:

function testEcho(message){
     window.JSInterface.doEchoTest(message);
}

Android:

myWebView.loadUrl("javascript:testEcho('Hello World!')");

Yes, Android rocks at this actually.

I'm posting example code from a random example app that I made but this should get you going.

Lets start by supposing you have to global variable to your webview controlling class:

String name = "John";
String lastName = "Doe";

In the Android class controlling the webview (during onCreate() or when you setup your webview):

webView.addJavascriptInterface(new JavaScriptInterface(), "android")

Inner class to your webview controlling class:

/**
* Sets up the interface for getting access to Latitude and Longitude data
* from device
**/
private class JavaScriptInterface {
    public String getName() {
        return name;
    }
    public String getLastName() {
        return lastName;
    }
    public void onJavascriptButtonPushed(String text) {
        Toast.makeText(WebViewControllingClass.this, "The text of the pressed button is: "+text,Toast.LENGTH_SHORT).show();
    }
}

Thats it for the Android side, now to use it through Javascript:

To get the name and lastname through JavaScript:

if (window.android){
    name = window.android.getName();
    lastName = window.android.getLastName();
}

If you want to raise the toast, set a javascript whose function would be:

if (window.android){
    window.android.onJavascriptButtonPushed("Hello");
}

Edit:

I havent tried this, but a search yielded this, try it out, it should work.

If you want to use it in the other direction:

JS:

function testEcho(message){
     window.JSInterface.doEchoTest(message);
}

Android:

myWebView.loadUrl("javascript:testEcho('Hello World!')");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文