I have a Service that I am running in the background every minute. It's an IntentService and I start it like this:
That works, my Service is being run every minute (give or take a couple seconds for some reason) whether the app is up, closed, the phone is sleeping, whatever.
My problem is, I'm not exactly sure how I should be getting the current location from this Service. I'm attempting to use LocationListener and a mix of getLastKnownLocation and hoping onLocationChanged will be called.
I think my problem is, onLocationChanged is barely getting called. Everytime the service runs and I do getLastKnownLocation I send a message to my database to check, I send a different message everytime onLocationChanged is called. I went to the store and back and getLastKnownLocation was called a few times, but onLocationChanged was only called once and it was incorrect. getLastKnownLocation always seems to be incorrect to.
This is what I'm doing:
So what am I doing wrong? I'm sure plenty...
Code:
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();
}
That works, my Service is being run every minute (give or take a couple seconds for some reason) whether the app is up, closed, the phone is sleeping, whatever.
My problem is, I'm not exactly sure how I should be getting the current location from this Service. I'm attempting to use LocationListener and a mix of getLastKnownLocation and hoping onLocationChanged will be called.
I think my problem is, onLocationChanged is barely getting called. Everytime the service runs and I do getLastKnownLocation I send a message to my database to check, I send a different message everytime onLocationChanged is called. I went to the store and back and getLastKnownLocation was called a few times, but onLocationChanged was only called once and it was incorrect. getLastKnownLocation always seems to be incorrect to.
This is what I'm doing:
Code:
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();
}
}
}
Code:
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,0,0, 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,0,0, 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;
}
Code:
@Override
public void onLocationChanged(Location location) {
if(location == null) {
saveLocation(-8.0, -8.0, "nothing_listener", "nothing_listener", "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(location.getLatitude(), location.getLongitude(), city +"_listener", state +"_listener", country +"_listener");
} catch (IOException e) {
e.printStackTrace();
}
}
}
So what am I doing wrong? I'm sure plenty...