Skip to first unread message

Andrius Dapševičius

unread,
Sep 28, 2013, 11:39:40 AM9/28/13
to codenameone...@googlegroups.com
We are currently trying to get current device location, but all we get is a log message "bestProvider null" or "bestProvider gps" depending on device and no connection, blocking thread does not end. lm.getLastKnownLocation() returns outdated location.

                LocationManager lm = LocationManager.getLocationManager();
                InfiniteProgress inf = new InfiniteProgress();
                final Dialog progress = inf.showInifiniteBlocking();

                MyLocationListener m = new MyLocationListener();
                lm.setLocationListener(m);
                try{
                System.out.println("Current location: " + lm.getCurrentLocation());// This code does not change anything.
                } catch (Exception e){}
                Display.getInstance().invokeAndBlock(m);
                System.out.println("Baigta");
                progress.dispose();

                showMeOnMap();

                lm.setLocationListener(null);
                int status = lm.getStatus();

LocationListener source code. From other similar thread:

class MyLocationListener implements LocationListener, Runnable {

        boolean locationReady;
        int status;

        public void locationUpdated(Location loc) {
            locationReady = true;
        }

        public void providerStateChanged(int newState) {
            // check for unavailable errors etc.
            status = newState;
            locationReady = true;
        }

        public void run() {
            // I recommend adding a timeout and setting the status to an error value for timeout
            while (!locationReady) {
                
                // sleep is find here because this isn't the EDT
                try {
                    System.out.println("Sleeping");
                    Thread.sleep(100);
                } catch (InterruptedException err) {
                }
            }
        }
    }

Shai Almog

unread,
Sep 28, 2013, 3:17:12 PM9/28/13
to codenameone...@googlegroups.com, aaaaa...@gmail.com
Are you outside?
If not you will get a broken location from GPS and it will keep looking. See the comment in the code (which I think is copied from code I or Chen wrote, looks like my style though) where it says you should implement a timeout.
Reply all
Reply to author
Forward
0 new messages