PhoneGap - 自定义插件无法与PhoneGap Build一起使用

时间:2014-09-23 22:18:26

标签: cordova plugins

我正在尝试为Cordova应用程序创建并添加插件。此时所有插件都返回一个警告框和一个带有一些文本的callbackContext。当我通过CLI使用“cordova run android”构建应用程序时,我可以成功调用插件并返回文本,但是当我通过PhoneGap将我的应用程序转换为APK时,插件不再有效。有谁知道为什么会这样?我已经广泛阅读了Cordova和PhoneGap文档,并使用已发布的PhoneGap插件中的代码作为指导,但似乎无法弄清楚这一点。

非常感谢任何帮助。

plugin.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.assettagz.cordova.plugin.scan"
version="1.0.0">

<js-module src="www/Scan.js" name="Scan">
    <clobbers target="scan" />
</js-module>

<!-- android -->
<platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
        <feature name="Scan" >
            <param name="android-package" value="com.assettagz.cordova.plugin.scan.Scan"/>
        </feature>
    </config-file>

    <source-file src="src/android/Scan.java" target-dir="src/com/assettagz/cordova/plugin/scan" />
</platform>

插件Javascript文件:

var exec = require('cordova/exec');

var Scan =
{
read : function( success, error )
{
    exec(success, error, "Scan", "read", [])
}
}

module.exports = Scan;

插件Java文件:

package com.assettagz.cordova.plugin.scan;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Message;
import android.util.Log;


public class Scan extends CordovaPlugin {

private CallbackContext callbackContext;

/**
 * Constructor.
 */
public Scan() {

}

@Override
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{

    if( action.equals("read") )
    {

        AlertDialog.Builder builder1 = new AlertDialog.Builder(cordova.getActivity());
        builder1.setMessage("success");
        AlertDialog alert11 = builder1.create();
        alert11.show();

        this.callbackContext = callbackContext;
        callbackContext.success("success");

        return true;
    }
    return false;
}

的index.html:

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title></title>
<link rel="stylesheet"  href="vendors/jquery.mobile/jquery.mobile-1.4.3.min.css">
<link rel="stylesheet" href="css.css" />
<script src="vendors/jquery/jquery.js"></script>
<script src="vendors/jquery.mobile/jquery.mobile-1.4.3.min.js"></script>
<script src="vendors/angular.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>  
<script>
    function fireItUp() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {            
        jQuery(input).bind('tap',function(event){
            callScan();
        });
    }

    var success = function(status) {
        alert('Message: ' + status);
    }

    var error = function(status) {
        alert('Error: ' + status);
    }

    function callScan(){
        scan.read( success, error );
    }
    </script>
</head>

<body onload="fireItUp()">
    <button class="ui-btn ui-corner-all ui-btn-b" onclick="callScan();">Scan</button>
</body>

1 个答案:

答案 0 :(得分:2)

除非您将插件提交给PhoneGap Build并批准,否则您无法在PhoneGap Build上使用自定义插件。通过这样做,您的插件将被添加到可用插件的第三方部分。目前无法使用PhoneGap Build

添加私人自定义插件