【发布时间】: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”事件之后运行您的代码?