让 PhoneGap 插件正常工作

Getting PhoneGap plugins working

我昨天在 tutorials and using the PhoneGap Developer App 之后创建了我的第一个 phonegap 应用程序。现在我正在尝试让插件工作,我不确定我是在某处遗漏了一个步骤还是完全错误。

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

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

现在我已经尝试安装手电筒插件,看看能否让插件正常工作。

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"

我是否遗漏了任何步骤或是否有其他方法可以做到?

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

应该看起来像这样。

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

我摆脱了 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 工作完美。