getPosition() the right strategy in getting GPS

By: Fazal Emailed: 1767 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

it 's great

View Tutorial          By: tanawat bhtk at 2008-03-19 22:17:53
2. View Comment

your web site is very useful for students

View Tutorial          By: aathithi at 2008-06-22 22:41:07

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Fazal
The Tag Life Cycle with Attributes in JSP
Using Shift Operators in C
IncludeAction in Struts
typedef example program in C
Arithmetic Evaluation Using the Expression Language in JSP
Tutorial on Complicated Declarations in C
Static Variables in C
getPosition() the right strategy in getting GPS
Precedence and Order of Evaluation in C
File Copying in C
JSF Life Cycle
Getting User Input Using cin in C++
UDP Datagram sample in J2ME
GUI components and menu based J2ME Applications.
A Serialization Example in Java

More Tutorials in J2ME
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using List to create a Menu and Menu items in J2ME
Using alerts and tickers in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
List of GPRS Access points for all countries

More Latest News
Most Viewed Articles (in J2ME )
Sample J2ME code that shows various functionality of RMS.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
StringItem sample program in J2ME
Sample program to draw a rectangle in J2ME
translate() Sample program in J2ME
'double buffering' Sample program in J2ME
getPosition() the right strategy in getting GPS
J2ME Canvas sample to show games programming in J2ME
Using alerts and tickers in J2ME
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Y.S. Sun Green Building Research Center
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
Most Emailed Articles (in J2ME)
How to load J2ME applications to the IDEN handsets
GUI components and menu based J2ME Applications.
Ticker sample program in J2ME
Using List to create a Menu and Menu items in J2ME
What is J2ME?
'LINK.EXE' is not recognized as an internal or ext
DateField sample program in J2ME
Sample program to draw a rectangle in J2ME
Types of configurations in J2ME
Types of Profiles in J2ME
A sample J2ME Auto-resume program.
getPosition() the right strategy in getting GPS
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
What is J2ME?