Programming Tutorials

getPosition() the right strategy in getting GPS

By: Fazal in J2ME Tutorials on 2007-09-22  

This article describes using the getPosition() method to get the GPS position in the background. I call this method from a separate thread like documentation recomends, so I can show a 'Cancel' button in the display in case I want to cancel the GPS request. The thing is that I don't know how to make getPosition() function to stop requesting. I want getPosition() to stop executing when I push 'Cancel' button. How do I do that? I'm sure there is a way, because the built-in app to get GPS position stops when I cancel.

If this is your problem then here are ways to do it.

The developer guide (p 489) says that closing the PositionConnection (from another thread) will cause the call to unblock. I have not done this in a while and don't have my code handy but if this doesn't work someone will speak up. At any rate, if you make the Cancel commandAction event close the PositionConnection, then that should do it.

Other strategies:

You could always make "cancel" do nothing except return back to your main screen, and allow the connection attempt to continue in the background. There are many ways to code this, but the bottom line is that if you think a position may be needed a short time later, or a relatively recent position will suffice, you could let the connection attempt complete in the background, and if it completes successfully, store the result.

If you think the user might attempt a position again a short time later, this would be a great strategy because maybe they just didn't wait long enough. You can silently continue to try to get a connection in the background. If you are successful you can decide whether or not to keep the receiver "hot" by making additional requests, until the user tries again or you stop after a while to conserve battery. Or when you get the fix you can notify the user that it is available. If you don't get the fix, no biggie, the user will simply try again. If you are still trying to get the fix when the user tries again, you should be able to cancel the current fix by closing the PositionConnection and then reopening it. Again, the idea here is to separate UI activity from location activity and to provide an event communication mechanism between the two. There are many on this list who can offer something more precise.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)