Blackberry - LocationProvider.getLocation () freezes

I wrote a very simple application that queries LocationProvider for a location and prints it to System.out. This works great in a simulator. However, when I run it on my Blackberry device, the getLocation call seems to hang indefinitely. I run the code in a separate thread that just gets the provider and requests it. I tried this with null criteria (which should give me default permissions?), As well as the criteria that Assist should provide, and then Standalone. I have included my code below. When I run this on my device, it hangs when calling getLocation.Here my code is below .. plzz says that I can do it 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();
}
+3
2

:


,

locationProvider.getLocation(-1) 

, -1, -. :

    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());
    }

, System.out.println(text) ,

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

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

-1 , . , , , - , 15 .

, , , , GPS , ...

, ?

0

Source: https://habr.com/ru/post/1719601/


All Articles