LocationListener does not receive updates when BrowserField 2 renders in 6.0

I have an application that calls a mobile web site using BrowserField 2.  The application needs make the current GPS position to the mobile website with each request to the site.

I used code MultipleFixDemo to get a LocationListener job.  Everything worked very well in the Simulator and all manual updates of location were received by the LocationListener in the application.

I changed the display to include a component BrowserField 2 and the LocationListener does not update the location of the Simulator.

Y at - it something I need to do something different when the user interface includes the BrowserField.  Can I have some kind of thread problem?  I only tested on OS 6.0 in the Simulator.  I don't have access to a real device.

In the code below, I have two defined screens.

If the pushScreen (new MultipleFixScreen() is uncommented and the pushScreen (new BrowserFieldScreen() is commented out, the LocationListener receives updates.)

If the pushScreen (newMultpleFixScreen () is commented out and the pushScreen (new BrowserFieldScreen() is uncommented, the LocationListener gets no updates.)

Here is my code:

public class MultipleFixDemo extends UiApplication
{
    private double longitude;
    private double latitude;
    BlackBerryLocationProvider myProvider;

    public static void main(String[] args)
    {
        new MultipleFixDemo().enterEventDispatcher();
    }

    public MultipleFixDemo()
    {
        startLocationUpdate();

        pushScreen(new BrowserFieldScreen());
        //pushScreen(new MultipleFixScreen());
    }

   private void startLocationUpdate()
    {
        try
        {
            BlackBerryCriteria myCriteria = new BlackBerryCriteria();
            myCriteria.enableGeolocationWithGPS(BlackBerryCriteria.FASTEST_FIX_PREFERRED);
            try
            {
                myProvider = (BlackBerryLocationProvider)LocationProvider.getInstance(myCriteria);

                if ( myProvider == null )
                {
                    Runnable showUnsupportedDialog = new Runnable()
                    {
                        public void run() {
                            Dialog.alert("Location service unsupported, exiting...");
                            System.exit( 1 );
                        }
                    };
                    invokeLater( showUnsupportedDialog );
                }
                else
                {
                    myProvider.setLocationListener(new LocationListenerImpl(), 2, -1, -1);

                }
            }
            catch (LocationException le)
            {
                System.err.println("Failed to retrieve a location provider");
                System.err.println(le);
                System.exit(0);
            }
        }
        catch (UnsupportedOperationException ue)
        {
            System.err.println("Require mode is unavailable");
            System.err.println(ue);
            System.exit(0);
        }
        return;
    }

    private class LocationListenerImpl implements LocationListener
    {
        public void locationUpdated(LocationProvider provider, Location location)
        {
            System.out.println("update location called");
            if(location.isValid())
            {
                longitude = location.getQualifiedCoordinates().getLongitude();
                latitude = location.getQualifiedCoordinates().getLatitude();
                float altitude = location.getQualifiedCoordinates().getAltitude();

                StringBuffer sb = new StringBuffer();
                sb.append("Longitude: ");
                sb.append(longitude);
                sb.append("\n");
                sb.append("Latitude: ");
                sb.append(latitude);
                sb.append("\n");
                sb.append("Altitude: ");
                sb.append(altitude);
                sb.append(" m");
                System.out.println("!!!!!!!Location update: " + sb.toString());
                //MultipleFixDemo.this.updateLocationScreen(sb.toString());
            }
        }

        public void providerStateChanged(LocationProvider provider, int newState)
        {
            System.out.println("providerStateChanged called");
            // Not implemented
        }
    }

    class BrowserFieldScreen extends MainScreen
    {
        private BrowserField _browserField;
        private boolean _documentLoaded = false;
        private BrowserFieldRequest _request;

        /**
         * Creates a new BrowserFieldScreen object
         * @param request The URI of the content to display in this BrowserFieldScreen
         * @param enableScriptMenu True if a context menu is to be created for this BrowserFieldScreen instance, false otherwise
         */
        public BrowserFieldScreen()
        {
            super(Screen.HORIZONTAL_SCROLL | DEFAULT_CLOSE | DEFAULT_MENU);
            BrowserFieldRequest request = new BrowserFieldRequest("http://www.google.com");

            BrowserFieldConfig config = new BrowserFieldConfig();
            _browserField = new BrowserField(config);
            _browserField.addListener(new DemoBrowserListener());
            add(_browserField);
            _request = request;
        }    

        /**
         * @see Screen#onUiEngineAttached(boolean)
         */
        protected void onUiEngineAttached(boolean attached)
        {
            if(attached)
            {
                try
                {
                    _browserField.requestContent(_request);
                }
                catch(Exception e)
                {
                    deleteAll();
                    add(new LabelField("ERROR:\n\n"));
                    add(new LabelField(e.getMessage()));
                }
            }
        }

        /**
         * Returns this screen's BrowserField object
         * @return This screen's BrowserField object
         */
        public BrowserField getBrowserField()
        {
            return _browserField;
        }

        /**
         * A class to listen for BrowserField events
         */
        private class DemoBrowserListener extends BrowserFieldListener
        {
            /**
             * @see BrowserFieldListener#documentCreated(BrowserField, ScriptEngine, Document)
             */
            public void documentLoaded(BrowserField browserField, Document document)
            {
                _documentLoaded = true;
            }
        }
    }
    private final static class MultipleFixScreen extends MainScreen
    {
        MultipleFixScreen()
        {
            super(DEFAULT_CLOSE | DEFAULT_MENU);

            RichTextField instructions = new RichTextField("Waiting for location update...",Field.NON_FOCUSABLE);
            this.add(instructions);
        }
    }
}

Any suggestions would be greatly appreciated.

Thank you

Jackie

This is a limitation of the BrowserField.  The BrowserField also recorded a LocationListener that is use to power the GPS updates via JavaScript to pages it restores.  This LocationListener / that your application has already registered (given that the application can only record 1).

There are two ways you can work around this problem.  You can save your LocationListener again after posting the BrowserField.  Note that if you take this approach methods JavaScript GPS won't within your BrowserField.

The second approach is to poll for updates location, instead of using a LocationListener.  It should continue to work after posting a BrowserField.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for