phonegap deviceReady not called in android

I found several other folks via a google search that experience the same problem.  To summarize, the deviceReady function IS being called, it just is being called super-fast, before your handler is setup to respond to the event.

Here is how I fixed it for the spotmouth android app. I test for the presence of the navigator.device object.  If it's not there, I register my callback. If there is already present, than the deviceReady event has already been called, and I just call it myself.

Html:

<body onload="onBodyLoad()">

</body>

 

Javascript:

 

    function onBodyLoad()
    {
   
        //if phonegap, need to toggle these
         if (typeof navigator.device == "undefined"){
              document.addEventListener("deviceready", onDeviceReady, false);
         } else {
         onDeviceReady();
         }

    }
   
    /* When this function is called, PhoneGap has been initialized and is ready to roll */
    function onDeviceReady()
    {
        // do your thing!
        runApp();
    }

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options