【问题标题】:Unable to get Device Detail in Phonegap application无法在 Phonegap 应用程序中获取设备详细信息
【发布时间】:2014-03-05 09:40:41
【问题描述】:

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

我已经在 config.xml 文件中输入了

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

当我在 Phonegap 上编译后在我的 android 手机中运行该应用程序时

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;
        }
    }
};

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

【问题讨论】:

  • 哪个版本的科尔多瓦?
  • 尝试从 CLI 添加插件,然后运行此命令 cordova build android
  • 我已经从命令行添加了插件,我正在使用 PhoneGap Cloud Build 来编译我的应用程序。
  • 我已经尝试了所有方法,但仍然无法正常工作......
  • 您是否在“deviceready”事件之后运行您的代码?

标签: android cordova


【解决方案1】:

我的工作。

无需放入 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 manifest 中使用 &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;

【讨论】:

  • 我使用的和上面一样。但它显示设备未定义
【解决方案2】:

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

要添加设备插件,你不能在config.xml中添加&lt;feature&gt;这行,而是:

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

phonegap build 中的插件使用请参考this page

【讨论】:

    【解决方案3】:

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

    1. 要么

      • 删除您的平台 (platform remove &lt;platform&gt;)
      • 通过命令行添加插件
      • 再次添加平台 (platform add &lt;platform&gt;)

    手动将本机插件文件复制到平台文件夹内相应的插件文件夹中。这样您就不必删除您的平台并丢失您对本机项目所做的所有更改。

    顺便说一句,当您通过命令行添加插件时,您不必弄乱 config.xml,因为这应该是自动完成的。

    【讨论】:

      【解决方案4】:

      此代码正在运行:

            <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标签下面添加&lt;div id="demo"&gt;&lt;/div&gt;

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

      【讨论】:

        【解决方案5】:

        AndroidManifest.xml中添加以下权限

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

        【讨论】:

        • 我没有任何 AndroidManifest.xml 文件。我正在从 phonegap build 构建我的项目。所以我只有 config.xml
        猜你喜欢
        • 1970-01-01
        • 2018-03-23
        • 2015-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多