I have a Service which is running every minute and is supposed to constantly track location. The Service implements LocationListener. I feel like I'm not understanding this correctly though because I'm not actually using the LocationListener methods:
onLocationChanged
onProviderEnabled
onProviderDisabled
etc
I have those methods in my Class but I don't actually do anything with them. All I'm doing is every time my service runs I call LocationManager.getLastKnownLocation for both GPS and Network Provider depending on which ones are available.
This is what my code looks like:
Service runs every minute:
Intent i = new Intent(context, GPSTracker.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(isOn) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), POLL_INTERVAL, pi);
} else {
alarmManager.cancel(pi);
pi.cancel();
}
I handle the Intent:
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "In onHandleIntent!");
Location currentLocation = getLocation(this);
if(currentLocation == null) {
saveLocation(-8.0, -8.0, "nothing", "nothing", "nothing");
} else {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryCode();
String state = addresses.get(0).getAdminArea();
saveLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), city, state, country);
} catch (IOException e) {
e.printStackTrace();
}
}
}
And getLocation() does the real work:
public Location getLocation(Context context) {
try {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
Something doesn't seem right though. For one, like I said I don't use any LocationListener methods, and also it doesn't seem to be very accurate. When I test on my phone sitting at my computer desk it gets it spot on every time. However, today I had this running on my phone once at work and another time when I was walking down the street and the locations it got for these places was like a mile away from both of them. Weirdly, it got the exact same wrong location for both of these places. These were both with Network providers as I didn't have my GPS on.
What am I doing wrong, and if I'm not doing anything wrong, how can I get this to be more accurate?