0

We are trying to develop a phonegap app with GWT, phonegap 2.4, gwtphonegap 2.4. Now we are testing it against Android 4.2. We have used the geopositioning stuff and it works like a charm in browser and mobile, but when we try to use the Globalization for taking the system's locale it doesn't work. The onPhneGapAvailable event never fires and if we call getGlobalization directly it says it is undefined.

We call this method from a onPhoneGapAvailable event:

  private void detectLanguage() {
        //if it is in a mobile take the language from the OS
        if (phoneGap.isPhoneGapDevice()) {
            Window.alert("detecting language");
            phoneGap.getGlobalization().getLocaleName(new GlobalizationCallback<CLocale, GlobalizationError>() {

                @Override
                public void onSuccess(CLocale s) {
                  ...
                }

                @Override
                public void onFailure(GlobalizationError f) {
                  ....
                }
            });
        }
    }

After that: phoneGap.initializePhoneGap();

Our html:

<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title></title>
       <script type="text/javascript" language="javascript" src="js/cordova-2.4.0.js"></script>
       <meta name="gwt:property" content="locale=en">

       <script src="js/init.js"></script>


    <script type="text/javascript" language="javascript" src="asdf.mobileclient/asdf.mobileclient.nocache.js"></script>

    <script type="text/javascript">
        document.addEventListener("deviceready", (function(){ PhoneGap.available = true;}), false);
    </script>
  </head>

  <body>
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>

  </body>
</html>

And the configuration in the app (config.xml) sets the following plugin:

 <plugin name="Globalization" value="org.apache.cordova.Globalization"/>

Thanks in advance.

Pablo Castilla
  • 2,723
  • 2
  • 28
  • 33

3 Answers3

2

You always need to wait for the PhonegapAvailable event. GWT-Phonegap registers on the "deviceready" event and will fire the GWT event afterwards.

Accessing any part of the API without deviceready will not work.

Daniel Kurka
  • 7,973
  • 2
  • 24
  • 43
  • This is really strange. I have moved forward, now I try to read the globalization in the event phonegapavailable event and it doesn't work. Same message: "can not read property globalization of undefined" I don't understand what's happening. – Pablo Castilla Jun 23 '13 at 18:46
0

I've never used gwtphonegap so I don't know how initializePhoneGap works neither the onPhoneGapAvailable event, but I'm sure that document.addEventListener("deviceready", (function(){ PhoneGap.available = true;}), false); fires once cordova has fully loaded, so in your case PhoneGap.available = true; when your device is ready.

Maybe you have to check that PhoneGap.available is true instead of counting on onPhoneGapAvailable event.

gaepi
  • 404
  • 3
  • 14
0

I have found with GWT 2.5 and phonegap 3.2, the PhonegapAvailable event is not being handled. My work-around is to handle the deviceready event directly.

So in onModuleLoad(), I have:

phoneGap = GWT.create(PhoneGap.class);
phoneGap.initializePhoneGap();
setupOnDeviceReady();  // This needs to follow the call to initializePhoneGap()

Where setupOnDeviceReady() is:

private native void setupOnDeviceReady() /*-{
    var self = this;
    var cb = function() {self.@com.myapp.client.EntryClass::onDeviceReady()();};
$doc.addEventListener("deviceready", $entry(cb), false);
}-*/;

And onDeviceReady() contains whatever code requires phonegap to be initialized (i.e., whatever code would have been in the onPhoneGapAvailable handler.

Mike Godin
  • 3,727
  • 3
  • 27
  • 29