I’m trying to create an application that sends the user’s location to the database every 5 minutes, after that the user will be able to see it. There are few constraints:
-
If the user is not moving DO NOT generate another location. I do that by checking if the new location distance to the current location is more then 1800M (I use only locations with accuracy under 900 so the radious of possible locations is 1800).
-
If I can get a better location based on accuracy to my current location then use it.
So I will provide now my code and I would like to know what do you think about it and if it can be done better because it’s not working perfectly. If you want a clear question : Ignore my code, how do I implement those 2 points I mentioned.
Here is the code:
public void onStart(Intent intent, int startId)
{
...
this.timer.scheduleAtFixedRate(new Send(), d1, TEN_MINUTES/2);
}
...
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_MINUTES/2, 0, listener);
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TEN_MINUTES/2, 0, listener);
...
@Override
public void onLocationChanged(Location location)
{
handleLocation(location);
}
private void handleLocation(Location location)
{
if(isUsable(location))
{
this.isUsable = true;
this.newLocation = location;
}
}
public boolean isUsable(Location location)
{
if (mGeocoderAvailable)
{
this.address = reverseGeocode(location);
return location.hasAccuracy() && location.getAccuracy() < 900 && !this.address.equals("");
}
return false;
}
class Send extends TimerTask
{
boolean run = true;
@SuppressWarnings("deprecation")
public void run()
{
if(isUsable)
{
isUsable = false;
if(newLocation != null)
{
if(location != null)
{
if(location.getAccuracy() > newLocation.getAccuracy() + 100)
sendTask();
else
if(newLocation.distanceTo(location) > 1800)
sendTask();
}
else
sendTask();
}
}
}
}
The most important piece of code is the Send class of course and the isUsable method which defines my constraints.
Bugs that popped out is duplication of same address, that shouldn’t be since this condition should cover this case:
if(newLocation.distanceTo(location) > 1800)
And another thing is that I drove my car for 20 km’s with my phone and I didn’t receive any location in my db, only when I finished driving (after 20km)..
First of all, you probably want to have a variable uncertainty requirement.
Secondly, here is your command to start the timer.
This command isn’t right. You also might want to create an instance of Send before you send it to the timer.
Before you go out driving the app, I suggest you test key parts of it with a more frequent time in your test environment. Set the time to every 10 seconds, and check to make sure it is working correctly, that your math is correct, it is reporting a position, etc.
Lastly, your method for adding is this:
First of all, you shouldn’t use an else in this case, you should really combine all of the if statements into one common theme. I’m not quite sure either where it is getting location/newLocation from, you should probably make getting functions to them, bonus points if they are thread safe. Also, and this is probably key, you are always executing
sendTaskif there isn’t a valid location, I suspect that’s your key bug. Try replacing that code with this: