Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deviceready won't fire in Phonegap 1.0.0 on Android

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>
like image 371
sandstrom Avatar asked Aug 23 '11 20:08

sandstrom


1 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>
like image 76
sandstrom Avatar answered Oct 31 '22 01:10

sandstrom