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

Posted

技术标签:

【中文标题】需要从cordova插件-android调用js函数【英文标题】:Need to call js function from cordova plugin -android 【发布时间】:2015-12-15 08:26:44 【问题描述】:

我试图从我添加的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)]“未捕获的 ReferenceError:插件未定义”,来源: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
);

【问题讨论】:

你是否在 Cordova-plugin.js 中定义了你的插件? @SameerDonga 在文档中提到 cordova.js 会自动加载它 我说的是 Cordova-plugin.js 对此不了解更多,我只是参考了 tut 并做到了,你能指导更多..@SameerDonga @SameerDonga 我没有在 cordova-plugin.js 中添加它自动更新,请看我编辑的问题,我还错过了什么? html中的调用是否正确?对我来说似乎有些不对劲。 【参考方案1】:

这里是 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)
);

【讨论】:

嘿,谢谢回答,但请查看我编辑的问题和评论,我认为调用不正确,请查看一次。

以上是关于需要从cordova插件-android调用js函数的主要内容,如果未能解决你的问题,请参考以下文章

理清cordova插件的调用流程

调试 Cordova 插件

Android Cordova 插件开发之编写自己定义插件

Android Cordova 插件开发之编写自定义插件

添加新插件后,cordova_plugins.js 未更新

Cordova - 如何从 android 代码更改页面?