开发用于android的phonegap cordova 2.2.0插件时出错

时间:2012-12-09 07:50:33

标签: java javascript android cordova

我正在尝试为Android编写一个PhoneGap Cordova 2.2.0插件,但我遇到了问题,我不确定原因是什么。

这是我的JS:

PushCapure.js

var PushCapture = { 
        init : function () { 
            console.log('Attempting to call the PushCapture plugin');
            return cordova.exec(function() {
                alert('PushCapture was successful');
            }, function(error) {
                alert('PushCapture failed');
            }, "PushCapture", "capture", []);
        } 
};

现在这是我想要运行的本机代码

com.ron.camanon.PushCapture.java

package com.ron.camanon;

import org.apache.cordova.api.CordovaPlugin;
import android.util.Log;

public class PushCapture extends CordovaPlugin {  

    public void capture()
    {
        Log.i("PushCapture", "PushCapture called, capture video stream intended...");
    }
} 

这对我不起作用,我还将此行添加到 res / config.xml

<plugin name="PushCapture" value="com.ron.camanon.PushCapture"/>

当我尝试插件时,错误回调是唯一执行的操作。

我做错了什么?

这是适用于Android的Cordova 2.2.0

1 个答案:

答案 0 :(得分:2)

这不是插件在Android中如何工作,看起来你正在使用iOS模型,它调用某种方法。在Android中,插件的“方法”或特定命令将作为exec函数中的字符串发送,请参阅下面的代码示例Phonegap tutoral for plugins on Android

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        this.beep(args.getLong(0));
        callbackContext.success();
        return true;
    }
    return false;  // Returning false results in a "MethodNotFound" error.
}