本文介绍了最好的办法,从JS调用的活动方式与PhoneGap的3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打个电话从我的index.html在PhoneGap的使用从MainActivity一个本地方法。

I'm trying to make a phone call from my index.html in phonegap using a native method from MainActivity.

我使用PhoneGap的3.0和Android 4.3平台。我试过第二个答案的this后,但它不是在为这个版本。

I'm using phonegap 3.0 and android 4.3 platform. I tried the second answer on this post but it is not working for this versions.

我想知道什么是打通这是最好的方法?

推荐答案

您可以创建自定义插件调用从本机端的任何方法。创建一个单独的JavaScript文件,说customplugin.js,并把这个进去:

You can create a custom plugin to call any method from the native side. Create a separate JavaScript file, say customplugin.js, and put this into it:

var CustomPlugin = {};

CustomPlugin.callNativeMethod = function() {
    cordova.exec(null, null, "CustomPlugin", "callNativeMethod", []);
};

现在的本地Java端,创建一个新类并将其命名为CustomPlugin.java,再补充一点:

Now on the native Java side, create a new class and name it CustomPlugin.java, then add this:

package com.yourpackage;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;

import com.yourpackage.MainActivity;

public class CustomPlugin extends CordovaPlugin
{
    private static final String TAG   = "CustomPlugin";

    private CallbackContext callbackContext = null;
    private MainActivity activity = null;

    /** 
     * Override the plugin initialise method and set the Activity as an 
     * instance variable.
     */
    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) 
    {
        super.initialize(cordova, webView);

        // Set the Activity.
        this.activity = (MainActivity) cordova.getActivity();
    }

    /**
     * Here you can delegate any JavaScript methods. The "action" argument will contain the
     * name of the delegated method and the "args" will contain any arguments passed from the
     * JavaScript method.
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
    {
        this.callbackContext = callbackContext;

        Log.d(TAG, callbackContext.getCallbackId() + ": " + action);

        if (action.equals("callNativeMethod")) 
        {
            this.callNativeMethod();
        }
        else
        {
            return false;
        }

        return true;
    }

    private void callNativeMethod()
    {
        // Here we simply call the method from the Activity.
        this.activity.callActivityMethod();
    }
}

请确保您通过添加该线图中的config.xml文件中的插件:

Make sure you map the plugins in the config.xml file by adding this line:

...
<feature name="CustomPlugin">
    <param name="android-package" value="com.yourpackage.CustomPlugin" />
</feature>
...

现在调用该插件从您的index.html,你可以简单地调用您的JavaScript方法:

Now to call the plugin from your index.html you can simply call your JavaScript method:

CustomPlugin.callNativeMethod();

使用此方法将允许您设置多个自定义方法方便。欲了解更多信息,请检查PhoneGap的插件开发指南here.

这篇关于最好的办法,从JS调用的活动方式与PhoneGap的3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:01