传递并返回来自 javascript 和 android 的值,并用作手机间隙插件

发布于 2024-12-29 00:08:48 字数 85 浏览 3 评论 0原文

我想为手机创建一个插件,它可以在 javascript 和 android 之间传递并返回值。

有人可以提出关于如何做到这一点的任何想法吗?

I want to create a plugin for phone which pass and returns the value between javascript and android.

Can anybody suggest any ideas on how to do this?

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

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

发布评论

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

评论(2

2025-01-05 00:08:48

其实,这并不是很难。在这里,我将向您展示如何从页面内的 javascript 调用本机代码,反之亦然:

从 Web 视图中调用本机代码
创建 Web 视图时添加 javascript 接口(基本上是 java 类,其方法将在 Web 视图中通过 javascript 公开调用。javascript

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

接口类本身的定义(这是我从我的另一个答案中获取的示例类,并打开视频在本机意图中)

public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activiy) {
        this.activity = activiy;
    }

    public void startVideo(String videoAddress){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
        activity.startActivity(intent);
    }
}

现在,如果您想从页面的 HTML 中调用此代码,您可以提供以下方法:

<script>
    function playVideo(video){
        window.JSInterface.startVideo(video);
    }
</script>

很简单,不是吗?

从本机代码调用 javascript 代码
这也很简单,假设在 WebView 中加载的 HTML 代码中定义了 javascript 函数:

<script>
    function function(){
        //... do something
    }
</script>

然后通过本机代码中的 WebView 调用此函数,如下所示:

webView.loadUrl("javascript:function()");

Actually, this is not very difficult. Here, I will show you how to call native code from javascript within the page and vice-versa:

Calling native code from within web view:
When creating the web view add javascript interface (basically java class whose methods will be exposed to be called via javascript in the web view.

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

The definition of the javascript interface class itself (this is exemplary class I took from another answer of mine and opens video in native intent)

public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activiy) {
        this.activity = activiy;
    }

    public void startVideo(String videoAddress){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
        activity.startActivity(intent);
    }
}

Now if you want to call this code form the HTML of the page you provide the following method:

<script>
    function playVideo(video){
        window.JSInterface.startVideo(video);
    }
</script>

Easy isn't it?

Calling javascript code from native code:
This is also simple suppose in the code of the HTML loaded in WebView you have javascript function defined:

<script>
    function function(){
        //... do something
    }
</script>

Then you call this function through the WebView in the native code like that:

webView.loadUrl("javascript:function()");
烟沫凡尘 2025-01-05 00:08:48

这是教程用于创建 PhoneGap 插件。此外,ChildBrowser 插件的说明特别重要好的。

Here's a tutorial for creating a PhoneGap Plugin. Also the instructions for the ChildBrowser plugin are especially good.

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