使PhoneGap插件工作 [英] Getting PhoneGap plugins working

查看:183
本文介绍了使PhoneGap插件工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天在教程之后创建了我的第一个phonegap应用程序,并使用 PhoneGap开发人员应用程式。现在我想让插件工作,我不知道我是否错过了一个步骤或做错了。



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


$ b b

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



现在我

  phonegap cordova插件添加https:// github .com / EddyVerbruggen / Flashlight-PhoneGap-Plugin.git 
phonegap cordova prepare

index.html我加入了flashligh.js:

 < script type =text / javascriptsrc =cordova .js>< / script> 
< script type =text / javascriptsrc =js / plugins / Flashlight.js>< / script>
< script type =text / javascriptsrc =js / index.js>< / script>

在生成的 index.js 已添加一行以切换手电筒: window.plugins.flashlight.toggle();

  //更新接收事件上的DOM 
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);

}



我已重新启动应用程式, c>

输入 phonegap插件列表,我得到以下结果:

  cordova- -flashlight 3.0.0Flashlight
cordova-plugin-geolocation 1.0.1Geolocation
cordova-plugin-whitelist 1.0.0whitelist
/ pre>

我缺少任何步骤或有其他方法吗?

解决方案

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

  cordova创建应用程序za。 co.mycee.appMyCee App
cd app
cordova platforms add android
cordova platforms add ios

#add plugins
cordova plugin add cordova -plugin-device
cordova插件add cordova-plugin-console
cordova插件添加cordova插件对话框
cordova插件添加cordova插件地理位置

# on phond
cordova run android

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

  // onSuccess Callback 
//这个方法接受一个`Position`对象,它包含
//当前GPS坐标
//
function onSuccess(position){
var element = document.getElementById ');
element.innerHTML ='Latitude:'+ position.coords.latitude +'< br />'+
'经度:'+ position.coords.longitude +'< br /> '+
'< hr />'+ element.innerHTML;
}

// onError Callback接收一个PositionError对象
//
函数onError(error){
alert('code:'+ error。 code +'\\\
'+
'message:'+ error.message +'\\\
');
}

onDeviceReady 我添加了这一行:

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

index.html 此html:

 < div id =geolocation> 
GEO位置在这里
< / div>

现在每次我运行 cordova run android ,我可以看到添加的GPS坐标。不知道为什么我不能得到它在Phonegap的工作,Cordova工作完美。


I've created my first phonegap app yesterday following the tutorials and using the PhoneGap Developer App. Now I'm trying to get plugins working, I'm not sure if I'm missing a step somewhere or doing it completely wrong.

On OSX, I installed nodejs using brew install nodejs followed by sudo npm install -g phonegap.

After phonegap was installed, I created a sample project phonegap create sample-app followed by cd sample-app and phonegap serve. I can now see the sample app inside the phonegap android app and if I make changes to the html / js, I can see it reflected immediately on my phone.

Now I've tried installing the flashlight plugin to see if I can get plugins to work.

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

Inside my index.html I've included the 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>

Inside the generated index.js, I've added a line to toggle the flashlight: 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);

}

I've restarted the app just to make sure (killing the process and running phonegap serve again), but I don't see any flashlight switching on.

Typing in phonegap plugins list, I get the following results:

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

Am I missing any steps or are there another way to do it?

解决方案

I got rid of phonegap and went straight cordova and the plugins work fine.

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

On the generated index.js I simply copied code snippets from the webpage:

// 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');
}

and inside onDeviceReady I've added this line:

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

In index.html I've added this html:

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

Now every time I run cordova run android, I can see the GPS coordinates being added. Not sure why I can't get it working in Phonegap, Cordova works perfect.

这篇关于使PhoneGap插件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆