无法在Phonegap应用程序中获取设备详细信息

时间:2014-03-05 09:40:41

标签: android cordova

我正在尝试使用cordova在手机屏幕应用程序中获取设备详细信息。 我在plugins文件夹中包含了org.apache.cordova.device插件

我在config.xml文件中输入了

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

当我在Android手机上运行应用程序后,在Phonegap上编译它

的index.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;
        }
    }
};

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

5 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:1)

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

要添加设备插件,您不能在config.xml中添加<feature>行,而是添加以下行:

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

有关phonegap版本中插件的使用,请参阅this page

答案 2 :(得分:1)

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

  1. 或者

    • 删除您的平台(platform remove <platform>
    • 通过命令行添加插件
    • 再次添加平台(platform add <platform>
  2. 手动将本机插件文件复制到platform文件夹中的相应插件文件夹中。这样您就不必删除您的平台并丢失对本机项目所做的所有更改。

    在旁注中,当你通过命令行添加插件时,你不必乱用你的config.xml,因为这应该自动完成。

答案 3 :(得分: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中,你必须提到在body标签下添加<div id="demo"></div>

为了清楚理解,请参阅&#39;完整示例&#39;部分链接: [1] http://docs.phonegap.com/en/3.3.0/cordova_device_device.md.html#Device

答案 4 :(得分:-1)

AndroidManifest.xml

中添加以下权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
相关问题