需要从cordova插件调用js函数-android

时间:2015-09-18 08:51:04

标签: android cordova cordova-plugins

我试图从我添加的cordova插件中调用一个javascript函数:

下面是我的代码:

添加pulgin后生成的js文件:

cordova.define("cordova-plugin-custom-camera-preview.PreviewCamera", function(require, exports, module) {  var previewCamera = {
    createEvent: function(successCallback, errorCallback) {
    }
}

var previewCamera = {
    createEvent: function(successCallback, errorCallback) {
                cordova.exec(successCallback,errorCallback, "CustomCameraPreview", "action_preview_camera_pulgin", "" );

     }
}
});

这就是我试图在html中调用上面的函数的方法:

<script type="text/javascript" src="../plugins/cordova-plugin-custom-camera-preview/PreviewCamera.js"></script>


          function customCamera( ) {
            PreviewCamera.previewCamera.createEvent(function()
             {
             alert("S");
             }, function()
             {
             alert("F");
             });
        }

问题:09-18 14:16:49.516 14182-14182 / com.rule14.bancmate I / chromium:[INFO:CONSOLE(318)]&#34; Uncaught ReferenceError:插件未定义&#34;,来源:file:///android_asset/www/pages/xxx.html(318)

编辑:  cordova-plugin.js的一部分:

   cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/cordova-plugin-camera/www/CameraConstants.js",
        "id": "cordova-plugin-camera.Camera",
        "clobbers": [
            "Camera"
        ]
    },
    {
        "file": "plugins/cordova-plugin-camera/www/CameraPopoverOptions.js",
        "id": "cordova-plugin-camera.CameraPopoverOptions",
        "clobbers": [
            "CameraPopoverOptions"
        ]
    },
    {
        "file": "plugins/cordova-plugin-camera/www/Camera.js",
        "id": "cordova-plugin-camera.camera",
        "clobbers": [
            "navigator.camera"
        ]
    },
    {
        "file": "plugins/cordova-plugin-camera/www/CameraPopoverHandle.js",
        "id": "cordova-plugin-camera.CameraPopoverHandle",
        "clobbers": [
            "CameraPopoverHandle"
        ]
    },
    {
        "file": "plugins/cordova-plugin-whitelist/whitelist.js",
        "id": "cordova-plugin-whitelist.whitelist",
        "runs": true
    },
    {
        "file": "plugins/cordova-plugin-custom-camera-preview/PreviewCamera.js",
        "id": "cordova-plugin-custom-camera-preview.PreviewCamera",
        "clobbers": [
            "PreviewCamera"
        ]
    }
];
module.exports.metadata = 
// TOP OF METADATA
{
    "cordova-plugin-camera": "1.2.0",
    "cordova-plugin-whitelist": "1.0.0",
    "cordova-plugin-custom-camera-preview": "1.0"
}
// BOTTOM OF METADATA
});

1 个答案:

答案 0 :(得分:0)

这是cordova_plugins.js 在此文件中添加插件引用,并将此文件调用到需要调用的位置。

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>


cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/com.paypal.cordova.mobilesdk/www/cdv-plugin-paypal-mobile-sdk.js",
        "id": "com.paypal.cordova.mobilesdk.PayPalMobile",
        "clobbers": [
            "PayPalMobile"
        ]
    },
    {
        "file": "plugins/cordova-plugin-whitelist/whitelist.js",
        "id": "cordova-plugin-whitelist.whitelist",
        "runs": true
    },
    {
        "file": "plugins/com.phonegap.plugins.barcodescanner/www/barcodescanner.js",
        "id": "com.phonegap.plugins.barcodescanner.BarcodeScanner",
        "clobbers": [
            "cordova.plugins.barcodeScanner"
        ]
    },
     {
         "file": "plugins/org.apache.cordova.camera/www/CameraConstants.js",
         "id": "org.apache.cordova.camera.Camera",
         "clobbers": [
             "Camera"
         ]
     },
    {
        "file": "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js",
        "id": "org.apache.cordova.camera.CameraPopoverOptions",
        "clobbers": [
            "CameraPopoverOptions"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.camera/www/Camera.js",
        "id": "org.apache.cordova.camera.camera",
        "clobbers": [
            "navigator.camera"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js",
        "id": "org.apache.cordova.camera.CameraPopoverHandle",
        "clobbers": [
            "CameraPopoverHandle"
        ]
    },
     {
         "file": "plugins/org.apache.cordova.media-capture/www/CaptureAudioOptions.js",
         "id": "org.apache.cordova.media-capture.CaptureAudioOptions",
         "clobbers": [
             "CaptureAudioOptions"
         ]
     },
    {
        "file": "plugins/org.apache.cordova.media-capture/www/CaptureImageOptions.js",
        "id": "org.apache.cordova.media-capture.CaptureImageOptions",
        "clobbers": [
            "CaptureImageOptions"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.media-capture/www/CaptureVideoOptions.js",
        "id": "org.apache.cordova.media-capture.CaptureVideoOptions",
        "clobbers": [
            "CaptureVideoOptions"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.media-capture/www/CaptureError.js",
        "id": "org.apache.cordova.media-capture.CaptureError",
        "clobbers": [
            "CaptureError"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.media-capture/www/MediaFileData.js",
        "id": "org.apache.cordova.media-capture.MediaFileData",
        "clobbers": [
            "MediaFileData"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.media-capture/www/MediaFile.js",
        "id": "org.apache.cordova.media-capture.MediaFile",
        "clobbers": [
            "MediaFile"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.media-capture/www/capture.js",
        "id": "org.apache.cordova.media-capture.capture",
        "clobbers": [
            "navigator.device.capture"
        ]
    },
       {
           "file": "plugins\SQLite-1.0.5\www\SQLitePlugin.js",
           "id": "org.pgsqlite.SQLitePlugin",
           "clobbers": [
               "SQLitePlugin"
           ]

       }
    ,
       {
           "file": "plugins\ionic-plugin-keyboard-master\www\keyboard.js",
           "id": "ionic-plugin-keyboard",
           "clobbers": [
               "IonicKeyboard"
           ]

       }
];
module.exports.metadata = 
// TOP OF METADATA
{
    "com.paypal.cordova.mobilesdk": "3.1.8",
    "cordova-plugin-whitelist": "1.0.0",
    "com.phonegap.plugins.barcodescanner": "2.0.1",
    "org.apache.cordova.camera": "0.3.6",
    "org.apache.cordova.media-capture": "0.3.6",
    "org.pgsqlite.SQLitePlugin": "1.0.5",
    "com.ionic.keyboard.IonicKeyboard": "1.0.5"
}
// BOTTOM OF METADATA
});

将此参考js

中的呼叫使用方法
function startScan() {

          cordova.plugins.barcodeScanner.scan(
              function (result) {                 
              },
              function (error) {                 
              }
          );
      }

以这种方式称呼你的功能

cordova.plugins.camerapreview.setOnPictureTakenHandler(function(result){
});