views:

274

answers:

3

Hi,

Using my Android application, when a user does a certain action, a background service is started that fetches the current GPS location and saves it in a database in addition of doing some other stuff. In that service, I use the requestLocationUpdates() from the LocationManager class and wait until the onLocationChanged() of my LocationListener (which is implemented by the service) is fired. But what is the best way to wait for the onLocationChanged to fire? Should I simply poll on a variable and wait until it is set? Any tips?

Note: I cannot simply write to the database in the onLocationChanged() because of some other stuff.

EDIT: To clearfy my situation, I can present an example similar to my case: Let's say user pushes a button and a method myMethod is fired which will return some object. Then, in this myMethod I will register for location updates from the GPS (using requestLocationChanged) and in addition wait for the users location and use it for something. In other words, myMethod cannot return before the location is present. I don't know how I can use onLocationChanged in this case.

+2  A: 

When the location is set, then onLocationChanged is fired so you don't need to poll any variable. Why can't you save the location in onLocationChanged?

BrennaSoft
Probably because the gps listener is in a background service and not his main process.
Segfault
When the user does the action that triggers the location fetching service, my application should get the current location before it proceeds. Let's say user pushes a button and a method myMethod is fired which will return some object. Then, in this myMethod I will register for location updates from the GPS (using requestLocationChanged) and in addition wait for the users location and use it for something. In other words, myMethod cannot return before the location is present. I don't know how I can use onLocationChanged in this case.
qtips
So you want to halt execution in myMethod while waiting for gps to sync and give you a location? You shouldn't do that. If that is what you need to do you should show a progress dialog that will get dismissed when onLocationChanged is fired. And then also in onLocationChanged finish whatever myMethod needs to do with the location.
BrennaSoft
A: 

What I do is set a view on the main screen to a progress bar of indeterminate length, set requestLocationUpdates() and just return back to the user. If you need to block the user until the location is resolved, then you can open a dialog on top of your activity that has no controls and a progress bar view. Then in your onLocationChanged callback you can dismiss that dialog and proceed with your business.

EDIT: I read your question again and I think I understand better. My first suggestion would be to see if you can do away with your background service. You don't have to worry about the app becoming unresponsive while waiting for a GPS lock because the GPS API is asynchronous. If you can't for whatever reason, then you will have to look at the message passing API to allow your background service to send a message to your main Activity when the GPS data is available. See the Developers guide.

Segfault
A: 

I think you should use wait/notify methods. You should have some lock object. You call lockObject.wait() in your method. And you call lockObject.notify() in onLocationChanged. This way your method will not return until onLocationChanged is received.

Actually the better solution would be to review your design and make your method asynchronous if it's possible.

Fedor