让PhoneGap插件正常工作

时间:2015-10-27 05:41:15

标签: cordova phonegap-plugins cordova-plugins

我昨天在tutorials后使用PhoneGap Developer App创建了我的第一个phonegap应用。现在我正在努力让插件正常工作,我不确定我是否在某个地方错过了一步或完全错误。

在OSX上,我使用brew install nodejs后跟sudo npm install -g phonegap安装了nodejs。

安装phonegap之后,我创建了一个示例项目phonegap create sample-app,然后是cd sample-appphonegap serve。我现在可以在phonegap android应用程序中看到示例应用程序,如果我对html / js进行了更改,我可以看到它立即反映在我的手机上。

现在我已经尝试安装手电筒插件,看看我是否可以让插件工作。

phonegap cordova plugin add https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin.git
phonegap cordova prepare

在我的index.html中,我已经包含了flashligh.js:

    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/plugins/Flashlight.js"></script>
    <script type="text/javascript" src="js/index.js"></script>

在生成的index.js内,我添加了一行来切换手电筒:window.plugins.flashlight.toggle();

// Update DOM on a Received Event
receivedEvent: function(id) {

    window.plugins.flashlight.toggle();

    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');
    receivedElement.innerHtml = navigator.geolocation.getCurrentPosition;

    console.log('Received Event: ' + id);

}

我已重新启动应用程序以确保(终止该进程并再次运行phonegap serve),但我没有看到任何手电筒开启。

输入phonegap plugins list,我得到以下结果:

cordova-plugin-flashlight 3.0.0 "Flashlight"
cordova-plugin-geolocation 1.0.1 "Geolocation"
cordova-plugin-whitelist 1.0.0 "Whitelist"

我错过了任何步骤,还是有其他方法可以做到这一点?

2 个答案:

答案 0 :(得分:0)

您能否确认已将插件添加到config.xml?

应该看起来像这样。

<gap:plugin name="cordova-plugin-flashlight" source="npm />

答案 1 :(得分:0)

我摆脱了phonegap并直接使用了cordova,插件工作正常。

cordova create app za.co.mycee.app "MyCee App"
cd app
cordova platforms add android
cordova platforms add ios

# add plugins
cordova plugin add cordova-plugin-device
cordova plugin add cordova-plugin-console
cordova plugin add cordova-plugin-dialogs
cordova plugin add cordova-plugin-geolocation

# run on phond
cordova run android

在生成的index.js上,我只是从网页上复制了代码段:

// onSuccess Callback
//   This method accepts a `Position` object, which contains
//   the current GPS coordinates
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                        'Longitude: ' + position.coords.longitude     + '<br />' +
                        '<hr />'      + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

onDeviceReady内部我添加了这一行:

var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });

index.html我添加了这个html:

<div id="geolocation">
                GEO Location Goes Here
            </div>

现在每次运行cordova run android时,我都会看到GPS坐标被添加。不知道为什么我无法在Phonegap中使用它,Cordova运作完美。