简体   繁体   中英

Upgraded to Phonegap 2.9.0 - deviceready not fired on Android

After upgrading from Phonegap 2.5.0 to 2.9.0 I can no longer get the deviceready event to fire.

Things I have tried:

  1. Adding the cordova_plugins.json file containing {}

  2. Looking for window.device in a timer (never seems to be initialized) (suggested here )

  3. Removing the code from cordova.js that atrempts loading of cordova_plugins.json and replacing with finishPluginLoading()

Nothing seems to be working. I am pulling my hair out over this one. Please help while I still have some left!


Here is my code so far, however it has been through numerous iterations, so it contains some dead code showing other avenues I have tried:

$(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() {
        ....

Your ondeviceready should be the very first thing called, not inside of a .ready.

It should look something like this:

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

Just stick with PhoneGap's deviceready instead of $(document).ready()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM