blackberrygpsjsr179

Blackberry - LocationProvider.getLocation() hangs on


I wrote a very simple application that asks the LocationProvider for a location and prints it to System.out. This works great in the simulator. However, when I run it on my blackberry device, the call to getLocation seems to hang indefinitely. I am running the code in a separate thread that simply gets a provider and asks for the location. I tried it with null Criteria (which should give me the defaults right?) as well as a Criteria that should provide Assist then Autonomous. I've included my code below. When I run this on my device it hangs on the call to getLocation.Here is my code below..plzz tell what i might be doing wrong...

public void getLocation() {
    Thread t = new Thread(new Runnable() {
        private double lat;
        private double lon;
        public void run() {
            Criteria cr = new Criteria();
            cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
            cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
            cr.setCostAllowed(false);
            cr.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
            cr.setPreferredResponseTime(1000);
            LocationProvider lp = null;
            try {
                lp = LocationProvider.getInstance(cr);
            } catch (LocationException e) {
            // System.out.println("*****************Exception" + e);
            }
            Coordinates c = null;
            if (lp == null) {
                UiApplication.getUiApplication().invokeLater(
                    new Runnable() {
                    public void run() {
                        Dialog.alert("GPS not supported!");
                        return;
                    }
                    });
            } else {
                // System.out.println("OK");
                switch (lp.getState()) {
                case LocationProvider.AVAILABLE:
                // System.out.println("Provider is AVAILABLE");
                        Location l = null;
                        try {
                        l = lp.getLocation(-1);
                    } catch (LocationException e) {
                    // System.out.println("***Location Exception caught "
                    // + e);
                    } catch (InterruptedException e) {
                    // System.out.println("***Interrupted Exception aught:"
                    // + e);
                    } catch (Exception e) {
                    // System.out.println("***Exception caught :"
                    // ;+ e);
                    }
                        if (l != null && l.isValid()) {
                        c = l.getQualifiedCoordinates();
                    }
                    if (c != null) {
                            lat = c.getLatitude();
                        lon = c.getLongitude();
                        System.out.println(lat + "  -  " + lon);
                    }
                }
            }
        }
    });
    t.start();
}

Solution

  • try to:


    Talking about hanging threads,

    locationProvider.getLocation(-1) 
    

    hangs because if you use -1, there will be no timeout. Try this:

        int timeout = 120;
        try {
            location = provider.getLocation(timeout);
        } catch (LocationException e) {
            System.out.println("Location timeout");
        } catch (InterruptedException e) {
            System.out.println("InterruptedException"+e.getMessage());
        }
    

    Also, if youre using System.out.println(text) in simulator, on device it would be better to

    getApplication().invokeLater(new Runnable(){
        public void run() {
            screen.add(new LabelField(text));           
        }
    });
    

    or

    getApplication().invokeLater(new Runnable(){
        public void run() {
            Dialog.inform(text);            
        }
    });