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?
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:
Your situation
You refer that you are using
LocationManagerand anHandler. None of these classes imply using a different thread.LocationManageruses the callbackonLocationChange()to run the code you define in the UI thread.Handlerruns 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.