2014-03-05 50 views
1

我试图在使用cordova的phonegap应用程序中获取设备详细信息。 我已经包括了org.apache.cordova.device插件的插件文件夹无法在Phonegap应用程序中获取设备详细信息

我已经做了在config.xml文件

<feature name="Device"> 
    <param name="android-package" value="org.apache.cordova.device" /> 
</feature> 

当我编译它的PhoneGap

运行后,在我的Android手机应用

索引条目html的

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <link href="css/kendo.common.min.css" rel="stylesheet" /> 
    <link href="css/kendo.silver.min.css" rel="stylesheet" /> 
    <link href="css/examples.css" rel="stylesheet" /> 
    <script type="text/javascript" src="js/jquery.min.js"></script> 
    <script type="text/javascript" src="cordova.js"></script> 
    <script type="text/javascript" src="device.js"></script> 
    <script type="text/javascript" src="phonegap.js"></script> 
    <script type="text/javascript" src="PushNotification.js"></script> 
    <script type="text/javascript" src="js/index.js"></script> 
    <script type="text/javascript"> 
     app.initialize(); 
    </script> 
</head> 
<body class="app"> 
    Application Started 
</body> 
</html> 

index.js

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicity call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     alert("Device Ready"); 
     try 
     { 
      alert(device.model); 
     } 
     catch(e) 
     { 
      alert(e.message); 
     } 
     //app.receivedEvent('deviceready'); 

    }, 
    // Update DOM on a Received Event 
    receivedEvent: function(id) { 
     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;'); 

     var pushNotification = window.plugins.pushNotification; 
     pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"XXX","ecb":"app.onNotificationGCM"}); 

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

    successHandler: function(result) { 
     alert('Callback Success! Result = '+result) 
    }, 

    errorHandler:function(error) { 
     alert(error); 
    }, 

    onNotificationGCM: function(e) { 
     switch(e.event) 
     { 
      case 'registered': 
       if (e.regid.length > 0) 
       { 
        console.log("Regid " + e.regid); 
        alert('registration id = '+e.regid); 
       } 
      break; 

      case 'message': 
       // this is the actual push notification. its format depends on the data model from the push server 
       alert('message = '+e.message+' msgcnt = '+e.msgcnt); 
      break; 

      case 'error': 
       alert('GCM error = '+e.msg); 
      break; 

      default: 
       alert('An unknown GCM event has occurred'); 
       break; 
     } 
    } 
}; 

我得到的设备是未定义的警报框。

+0

其中科尔多瓦版本? –

+0

其版本3.3.0 –

+0

尝试从CLI添加插件,然后运行此命令cordova build android –

回答

-1

添加下面的权限在AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
+1

我没有任何AndroidManifest.xml文件。我从phonegap构建我的项目。所以我只有config.xml –

2

矿工作。

无需放入index.js。你可以插入任何JavaScript文件没有问题。

我把这个脚本放在我的index.html中没有任何问题。

<script> 
alert(device.cordova); 
alert(device.model); 
alert(device.name); 
alert(device.platform); 
alert(device.uuid); 
alert(device.version); 
< /script> 

P.S:无需在您的Android清单中使用<uses-permission android:name="android.permission.READ_PHONE_STATE" />

+0

我已经使用上面的相同。但它显示设备是未定义的 –

1

您在config.xml中添加了错误的行以在phonegap build中添加插件。

添加的设备插件,你不能在config.xml中添加<feature>线路,但线路:

<gap:plugin name="org.apache.cordova.device" /> 

对于PhoneGap的构建插件使用,请参阅this page

1

我知道当你通过命令行添加它们并重建项目时,原生插件文件不会被复制到平台文件夹中。我设法通过两种方式来解决这个问题:

  1. 要么

    • 删除您的平台(platform remove <platform>
    • 通过命令行
    • 添加插件再次添加平台(platform add <platform>

手动将原生插件文件复制到平台文件夹内相应的插件文件夹中。这样你就不需要移除你的平台,并且不会丢失对原生项目所做的所有更改。

在旁注中,你不必乱搞你的配置。xml当你通过命令行添加插件时,这应该是自动完成的。

0

此代码工作:

 <script type="text/javascript"> 
     document.addEventListener("deviceready",onDeviceReady,false); 
     function onDeviceReady() { 
      var element = document.getElementById('demo'); 
      element.innerHTML = 'Device Model: ' + device.model + '<br />' + 
         'Device Cordova: ' + device.cordova + '<br />' + 
         'Device Platform: ' + device.platform + '<br />' + 
         'Device UUID: '  + device.uuid  + '<br />' + 
         'Device Version: ' + device.version + '<br />'; 
     </script> 

在HTML就不得不提到以下身体标记添加<div id="demo"></div>

为了清楚的了解,请参考在链接“完全实施例”部分: [1] http://docs.phonegap.com/en/3.3.0/cordova_device_device.md.html#Device

相关问题