差距:PhoneGap上的[“Device”,“getDeviceInfo”,“Device1231860141”]错误

时间:2015-04-12 05:13:49

标签: android cordova

我创建了一个phonegap应用,我添加了Android平台。但不幸的是,deviceready事件并没有被解雇。它在ripple模拟器上工作正常,但它不适用于chrome(桌面)和Android手机上。

如果我在chrome(桌面)或Android L手机上运行它,我会得到错误空白:

  

["装置"" getDeviceInfo"" Device1231860141"]

并且浏览器和NetBeans都会挂起。

我的cordova.js文件夹中有www个文件。我尝试删除它,但错误消失了,但deviceready仍然没有被解雇。

以下是我的代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=320, user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="js/libs/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css"/>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/libs/jquery/jquery.js"></script>
<script type="text/javascript">
function init(){
document.addEventListener("deviceready", onDeviceReady, true);
}
 function onDeviceReady() {
    startWatch();
}

// Start watching the acceleration
//
function startWatch() {

    // Update acceleration every 3 seconds
    var options = { frequency: 3000 };

    watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}

// Stop watching the acceleration
//
function stopWatch() {
    if (watchID) {
        navigator.accelerometer.clearWatch(watchID);
        watchID = null;
    }
}

// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
    var element = document.getElementById('accelerometer');
    element.innerHTML = 'Acceleration X: ' + acceleration.x         + '<br />' +
                        'Acceleration Y: ' + acceleration.y         + '<br />' +
                        'Acceleration Z: ' + acceleration.z         + '<br />' +
                        'Timestamp: '      + acceleration.timestamp + '<br />';
}

// onError: Failed to get the acceleration
//
function onError() {
    alert('onError!');
}
</script>
</head>
<body onload="init()">
</body>
</html>

1 个答案:

答案 0 :(得分:0)

deviceready 是一项无法在浏览器中使用的Cordova活动。

...
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, true);
function onDeviceReady() {
    startWatch();
}

...
</script>
</head>
<body onload="onDeviceReady()">
</body>
</html>