I am working on GPS Location service on Android. I have implemented my GPS location code in Android Service. Here is the snapshot of my location code. In my application, there are other threads which are working in parallel to send these location coordinates to the server.
public void onLocationChanged(Location loc) {
if (loc != null) {
synchronized (lock) {
currentLat = loc.getLatitude();
currentLon = loc.getLongitude();
if (loc.hasSpeed()) {
float speedInMeterSec = loc.getSpeed();
}
}
I am interested in synchronizing my location code so that when I am updating these values on the map, performing calculations or sending the data over network using these values, the issues of Dirty Read might not occur. The currentLat and currentLon variables are static in my service.
I would like to have your suggestions in improving the code quality, and specifically synchronizing my location code.
Thanks in advance
You actually don’t need to synchronize the location you just received in OnLocationChanged. But it seems like you want to synchronize currentLat and currentLong. The way you would do that is by making currentLat and currentLong private with public getters and setters and synchronizing the get/set functions. Such as:
Make sure currentLat itself is private static. Any other questions, please ask away!