0

My application have to catch gps coordinates and send them periodically using an handler. Within the onCreate method I do:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, this);

and with that the onLocationChanged starts working. At the end of the onCreate, I start an handler that every 2.5 seconds do some stuff. I think this could not be the right way to achive my target, because the onLocationChanged() is not executed in a separate thread, so if the handler executes the onLocation could not be executed.
EDIT
I do not know how to concurrently manage the retrieval of GPS coordinates (how to execute the onLocationChanged). The timer runs every 2.5 seconds a task and at the same time the onLocationChange have to set gps values that I need in the timer. I fear that there may be problems with the onLocationChange that may not be performed at all. I thought to use a AsyncTask, but how to execute it?

Ant4res
  • 1,217
  • 1
  • 18
  • 36
  • 2
    Can you please be more specific on what you want to achieve and on what is the issue? – Luis Dec 18 '12 at 11:26

2 Answers2

1

You can use the AsyncTask for fetching data.

refer this question.hope you will find the answer.

How To Get Location Using AsyncTask

Community
  • 1
  • 1
ShreeshaDas
  • 2,042
  • 2
  • 17
  • 36
  • Thanks, but where can I call the asyncTask execute, if separately I have the timer that have to handle those data? – Ant4res Dec 18 '12 at 12:01
1

Although you have edited your question still not clear to me what exactly is your concern.

I'll give you some background information about concurrency, and let's see if it helps...

What is it

First, concurrency means a software designed to have more then one thread. That doesn't means that more then one thread will run in simultaneous, which only happens if you have a device with more then one core. If the device only has one core, only one thread will run at a time, and the system will switch between them.

As soon as your system is designed to have more then one thread, you must ensure that all your code (and used libraries) that are dealing with shared objects are thread safe.

Why should I need it

The most common reansons why you would need to use threads are:

  • You have a heavy piece of code that could be split into parts, and you have a device with more then one core. In this case, you could have a performance benefict from having two (or more) simoultaneous running threads dealing with parts of the work.
  • You have a blocking operation in your code (i.e. read from a socket). This should be moved to a separate thread to avoid blocking all your program until socket.read() returns.
  • Finally and the most commun one in Android, any long run operation (i.e. more then a couple of seconds) should be moved to a different thread from he one used by the UI (user interface), to avoid pour user experience and/or the ANR error.

Your situation

You refer that you are using LocationManager and an Handler. None of these classes imply using a different thread.

LocationManager uses the callback onLocationChange() to run the code you define in the UI thread.

Handler runs the code in the thread where the handler is assigned to. So if you created your handler in the UI thread, the handler callback will be run there as well.

Only if you create a new Thread (or any other class that does the same) you have a real multi-thread app that requires you to be carefull with shared objects.

Hope it helps. Let me know if you need more clarification.

Regards.

Luis
  • 11,978
  • 3
  • 27
  • 35