从 Phonegap 2.5.0 升级到 2.9.0 后,我无法再触发 deviceready 事件。
我尝试过的事情:
添加包含
{}
的cordova_plugins.json
文件在计时器中查找
window.device
(似乎从未初始化)(建议 here )从
cordova.js
中删除尝试加载cordova_plugins.json
的代码,并替换为finishPluginLoading()
似乎没有什么效果。我正在为这个而揪头发。趁我还剩下一些,请帮忙!
<小时/>这是到目前为止我的代码,但是它已经经过了多次迭代,因此它包含一些死代码,显示了我尝试过的其他途径:
$(document).ready(function() {
function initializePhoneGap( success, failure ) {
var timer = window.setInterval( function () {
if ( window.device ) {
window.clearInterval( timer );
success();
}
}, 100 );
window.setTimeout( function () { //failsafe
if ( !window.device ) { //phonegap failed
window.clearInterval( timer );
failure();
};
}, 10000 ); //5 seconds
}
console.log( 'Waiting for launch...');
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
//$(document).on("deviceready", didFinishLaunching, false);
document.addEventListener("deviceready", didFinishLaunching, false);
//initializePhoneGap( function(){ console.log('Phonegap initialized'); didFinishLaunching() }, function(){ console.log('Phonegap timed out'); didFinishLaunching() } );
} else {
console.log('Skipping phonegap initialization');
didFinishLaunching();
}
function didFinishLaunching() {
....
最佳答案
你的 ondeviceready 应该是第一个被调用的东西,而不是在 .ready 中。
它应该看起来像这样:
<script>
document.addEventListener("deviceready", deviceIsReady, false);
function deviceIsReady() {
/*This is where all of you initialization code should go.
With PhoneGap the deviceready should be the first thing*/
}
</script>
只需坚持使用 PhoneGap 的 deviceready 而不是 $(document).ready()
关于javascript - 升级到 Phonegap 2.9.0 - deviceready 在 Android 上未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578432/