通和从JavaScript和android返回值和作为电话的间隙插件使用 [英] Pass and return the values from javascript and android and use as a phone gap plugin

查看:213
本文介绍了通和从JavaScript和android返回值和作为电话的间隙插件使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个插件的手机,它通过与返回的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?

推荐答案

其实这也不是很困难的。她,我会告诉你如何页面,反之亦然内调用本地code从JavaScript:

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

调用本机code从内部Web视图
当创建Web视图中添加的JavaScript接口(基本的Java,它的方法将被暴露通过JavaScript在Web视图被称为类。

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");

的JavaScript接口类本身的定义(这是实施例一类我从我的另一个答案,并在本地意图打开视频)

The definition of the javascript interface class itself (this is examplary 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);
    }
}

现在,如果哟希望让此code构成您提供以下方法页面的HTML:

Now if yo 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?

调用从本地code JavaScript的code
这也是您​​所定义的JavaScript函数的WebView加载htnl的code简单的假设:

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

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

然后你通过web视图中的原生code类的调用这个函数:

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

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

这篇关于通和从JavaScript和android返回值和作为电话的间隙插件使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆