Cancelling getCurrentLocationSync()

81 views
Skip to first unread message

cubiq1

unread,
Aug 4, 2014, 7:14:33 AM8/4/14
to codenameone...@googlegroups.com
Hi,

I know it's possible to add a timeout value to the above method but it is possible to cancel the method using code?

The reason I ask is because I'd like to add a "Cancel" button to my "Getting Location..." Dialog.

Many thanks

Diamond

unread,
Aug 4, 2014, 8:08:31 AM8/4/14
to codenameone...@googlegroups.com
How are you getting the location? are you using locationListenerr and did you block the process with InfiniteProgress?

 
If you post what you've tried, you will get help fast.

Here is another way to read location


public static Double Lat = 0.0;
public static Double Long = 0.0;

public void GetMyLocation() {
        final LocationManager lm = LocationManager.getLocationManager();
        InfiniteProgress ip = new InfiniteProgress();
        final Dialog ipDlg = ip.showInifiniteBlocking();
        Command back = new Command("Back") {
            @Override
            public void actionPerformed(ActionEvent ev) {
                ipDlg.dispose();
               //Here I get the device last known location when user press back
                Location loc = lm.getLastKnownLocation();
                Lat = loc.getLatitude();
                Long = loc.getLongitude() ;
            }
        };
        ipDlg.setBackCommand(back);
        class MyLocationListener implements LocationListener, Runnable {

            boolean locationReady;
           
            public void locationUpdated(Location loc) {
                locationReady = true;
                Lat = loc.getLatitude();
                Long = loc.getLongitude();
            }

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

            public void run() {
                while (!locationReady) {
                    try {
                        //set to end process after 20 seconds if current location is not found and use device last known location
                        Thread.sleep(20000);
                        ipDlg.dispose();
                        Location loc = lm.getLastKnownLocation();
                        Lat = loc.getLatitude();
                        Long = loc.getLongitude();
                    } catch (InterruptedException err) {

                    }
                }
            }
        }

        MyLocationListener m = new MyLocationListener();
        lm.setLocationListener(m);
        Display.getInstance().invokeAndBlock(m);
        

        ipDlg.dispose();
        lm.setLocationListener(null);
        
        Log.p("Lat: " + Lat);
        Log.p("Long: " + Long);

    } 

cubiq1

unread,
Aug 4, 2014, 10:57:15 AM8/4/14
to codenameone...@googlegroups.com
Thanks for the code.

I only need to get the location once so I'm use the following:

Location position = LocationManager.getLocationManager().getCurrentLocationSync(30000); // 30 second timeout

However, I'd like the user to be able to cancel the operation when it takes too long to complete.

Diamond Mubaarak

unread,
Aug 4, 2014, 1:39:53 PM8/4/14
to codenameone...@googlegroups.com
Try this


Location position = LocationManager.getLocationManager().getCurrentLocationSync(30000); // 30 second timeout
        Command back = new Command("Back") {
            @Override
            public void actionPerformed(ActionEvent ev) {
                Thread.currentThread().interrupt();
            }
        };
//depends on if you are getting locationon form show or by pressing a button, use one of this    
    c.getComponentForm().setBackCommand(back);

or 

f.setBackCommand(back);



--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/cxqn5DUWueM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/67b07e60-ff07-465a-8316-ab27202ac5a9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--


Kind Regards,
Diamond Mubaarak
Lead Software Developer.

Read about me here
View my profile on LinkedIn

Shai Almog

unread,
Aug 5, 2014, 1:27:40 AM8/5/14
to codenameone...@googlegroups.com
That won't work. You would be "interrupting" the EDT.
To cancel just set the location listener to null which will stop the listening process.

cubiq1

unread,
Aug 5, 2014, 4:19:42 AM8/5/14
to codenameone...@googlegroups.com
Thanks. This worked perfectly:

LocationManager.getLocationManager().setLocationListener(null);

Cheers

cubiq1

unread,
Aug 5, 2014, 4:31:27 AM8/5/14
to codenameone...@googlegroups.com
Hi, to add to my previous post. It seems as though even after setting the location listener to null, the code still pauses at the line:

        // Timeout after 30s
       
Location position = LocationManager.getLocationManager().getCurrentLocationSync(30000);

for the duration that the timeout was set for.

Is there a way to fix this?

Shai Almog

unread,
Aug 5, 2014, 9:32:54 AM8/5/14
to codenameone...@googlegroups.com
Hi,
not with that implementation.
You can implement your own location sync call to do that.

cubiq1

unread,
Aug 13, 2014, 6:06:43 AM8/13/14
to codenameone...@googlegroups.com
Hi,

So there's no way to cancel getCurrentLocationSync() other than setting a timeout duration?

Thanks

Shai Almog

unread,
Aug 13, 2014, 10:50:09 AM8/13/14
to codenameone...@googlegroups.com
Hi,
no.

cubiq1

unread,
Aug 17, 2014, 6:15:51 AM8/17/14
to codenameone...@googlegroups.com
Hi,

Would it be possible for you to add a method which allows users to cancel getCurrentLocationSync()? I would imagine many will find it useful.

Thanks

Shai Almog

unread,
Aug 17, 2014, 11:34:43 AM8/17/14
to codenameone...@googlegroups.com
Hi,
we probably won't. We have a lot on our plate right now and this just probably won't happen.
Reply all
Reply to author
Forward
0 new messages