通和从JavaScript和android返回值和作为电话的间隙插件使用间隙、返回值、插件、电话

由网友(忽如远行客)分享简介:我想创建一个插件的手机,它通过与返回的JavaScript和Android之间的值。 I want to create a plugin for phone which pass and returns the value between javascript and 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()");
阅读全文

相关推荐

最新文章