Home » Android » cordova – deviceready won't fire in Phonegap 1.0.0 on Android

cordova – deviceready won't fire in Phonegap 1.0.0 on Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’ve tried to setup Phonegap on Android and deviceready won’t fire. The reason is that DeviceInfo.uuid is always null/undefined.

It seems like the non-javascript parts of phonegap isn’t loaded correctly, but I can’t see exactly what. For everything outside the www directory I’m using the code provided in the sample directory of the phonegap download.

Anyone know what may be causing this?

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">

      <script type="text/javascript" charset="utf-8" src="javascripts/phonegap-1.0.0.js"></script>
    <script src="http://debug.phonegap.com/target/target-script-min.js#something"></script>

    <script type="text/javascript" charset="utf-8">

    function onBodyLoad() {
      var initialize = function() {
        window.console.log("deviceready||resume");
      };
      document.addEventListener("deviceready", initialize);
      document.addEventListener("resume", initialize);
      window.console.log("onBodyLoad!");
    }

    </script>
  </head>
  <body onload="onBodyLoad()">
  <h1>Herro World</h1>
  </body>
</html>
How to&Answers:

In case someone else stumble on this problem.

I hadn’t realized that phonegap-1.0.0.js is different for the iPhone and Android version. It has the same name, but the content is different. Thus, one must load the correct file. I solved it like this:

<script type="text/javascript">
    // Atrocious way of loading two diffent phonegap scripts, but other loading methods won't work.
    // also there shouldn't be two scripts to begin with -- so much for cross-platform.
    var userAgent = navigator.userAgent.toLowerCase();
    if (userAgent.match(/android/)) {
    document.write("<script type='text/javascript' src='javascripts\/phonegap-android-1.0.0.js'><\/script>");
  } else {
    document.write("<script type='text/javascript' src='javascripts\/phonegap-iphone-1.0.0.js'><\/script>");
  }
</script>

Answer:

If you want some function to execute when the device is ready do something like this

document.addEventListener("deviceready", onDeviceReady, true);
// PhoneGap is now ready

function onDeviceReady() {
// Write your code here
}

I am not sure why your code is not working.Try placing the document.addEventListener outside the scope of the function.