2014-03-05 49 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

相關問題