如何从cordova 3.x调用本机函数 [英] How to call native function from cordova 3.x

查看:18
本文介绍了如何从cordova 3.x调用本机函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从cordova/phonegap webview 调用原生函数,例如展示广告.

How can I call a native function from cordova/phonegap webview for example for showing an ad.

好的,我完全明白了,我会为你们所有不知道如何做的人写一些步骤(只是为了节省你一生中的 2 天:D)

OK I FUNALLY GOT IT and I'm gonna write some steps for all of you who don't know how to do that (just to spare 2 days of your lifetime :D)

A) 如果您只有 cordova/phonegap 并且希望从 js 调用,请执行以下操作:

A) if you have just cordova/phonegap and and wish to call from js do this:

1) 用您现有的 DroidGap Activity 替换以下代码.

1) Replace the following code with your existing DroidGap Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init(); // Calling this is necessary to make this work
    appView.addJavascriptInterface(this, "MainActivity");

    /* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */

    super.loadUrl("file:///android_asset/www/index.html");
}

2) 在当前(此)活动中添加自定义函数,如下所示.

2) Add the custom function in current (this) activity as following.

@JavascriptInterface
public void customFunctionCalled() {
    Log.e("Custom Function Called", "Custom Function Called");
}

3) 现在从您的 HTML/JavaScript 代码调用此函数,如下所示.

3) Now call this function from your HTML/JavaScript code as following.

 window.MainActivity.customFunctionCalled();

B.1) 如果你在 webview 中实现了 cordova/phonegap 并希望从 js 调用,请执行以下操作:(并想调用普通函数)

B.1) if you have cordova/phonegap implemented in a webview and wish to call from js do this: (and want to call normal function)

1) 将此添加到主 java 文件中:

1) Add this to the main java file:

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

2) 声明 JavaScriptInterface 类:

2) Declare the class JavaScriptInterface:

public class JavaScriptInterface {
    private Activity activity;

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

    @JavascriptInterface
    public void showLog(){
        Log.v("blah", "blah blah");
    }

}

3) 使用 `window.JSInterface.showLog() 从 js 调用它;

3) Call it from js with `window.JSInterface.showLog();

B.2) 如果你在 webview 中实现了 cordova/phonegap 并且希望从 js 调用(并且想要调用 UI 函数,比如 toast),请执行以下操作:

B.2) if you have cordova/phonegap implemented in a webview and wish to call from js (and want to call UI function, like a toast) do this:

1) 将此添加到主 java 文件中:

1) Add this to the main java file:

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

2) 声明 JavaScriptInterface 类:

2) Declare the class JavaScriptInterface:

public class JavaScriptInterface {
    private Activity activity;

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

    @JavascriptInterface
    public void myFunction()
    {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                //Code that interact with UI
                showToast();
            }
        });

    }

}

3) 在下面添加 toast 功能:

3) Add the toast function underneath:

public void showToast(){
    Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
               Toast.LENGTH_LONG).show();
}

4) 用 `window.JSInterface.myFunction();

4) Call it with `window.JSInterface.myFunction();

如您所见,如果您需要一个使用 UI 的函数,您需要将您的函数包装到 activity.runOnUiThread 中,以便它可以从 js 中调用.

As you see if you need a function that uses the UI you need to wrap your function into activity.runOnUiThread so that it can get called from js.

*如果您想从 java 调用 jquery 方法,请执行以下操作:

*If you want to call from java a jquery method do this:

Java:

cordova_webview.loadUrl("javascript:window.functionn()");

Javascript:

Javascript:

window.function = punish;

祝您有美好的一天!

推荐答案

在 .java 文件中

In the .java file

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.init(); // Calling this is necessary to make this work

    appView.addJavascriptInterface(this, "MainActivity");

    super.loadUrl(Config.getStartUrl());

}

在 JavaScript 中

In the javascript

window.MainActivity.customFunctionCalled();

这仅适用于 TargetSDK<17 .需要设置一个 androidManifest.xml targetSDK <17. 对于 TargetSDK >=17 ,我猜你需要创建一些自定义插件.我现在使用这个过程来降低我的目标 SDK.

This will only work on TargetSDK<17 . An androidManifest.xml targetSDK need to be set < 17. And for TargetSDK >=17 , I guess you'll need to create some custom plugin. I use this process lowering my target SDK for now.

这篇关于如何从cordova 3.x调用本机函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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