I am trying to implement a gps location listener that will stay on and continuously update in the background. I realize this is bad practice but I am doing this for testing purposes. Right now I have a location listener that gets called with getGPS(). It can also be killed with killGPS().
It works perfectly when im walking around and I click my get gps button which takes the latitude and longitude from my location parameter which I get from
GPSActivity.getGPS();
loc_listener = GPSActivity.loc_listener;
locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, loc_listener);
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
However in the background, sometimes it will say I’m still at my apartment even though I’ve been out for 3hours!
Why is my location listener not updating itself even though it turns on to find a new location every 10 mins?
Thanks
package com.cellphone;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSActivity extends Activity{
public static LocationListener loc_listener = null;
public static void getGPS() {
if (loc_listener == null) {
loc_listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
}
}
public static void killGPS(LocationManager locationManager) {
if (locationManager != null && loc_listener != null) {
locationManager.removeUpdates(loc_listener);
}
}
}
for location poller, here is simple & working example:
https://github.com/commonsguy/cwac-locpoll
you can keep state of your app by adding a function in Application context. So to say, location poller can be aware of app-state and skip gps action if app is not active.