Search code examples
androidservicelocationlocationmanager

Android: LocationManager service stops updating when set to NETWORK_PROVIDER


Right, the situation is as follows;

We have developed an app that checks the location of the user every 5-10 minutes for location specific content. To do so, I've created a Service to stick to the background (so it can update even when the app isn't directly in the foreground) in which a LocationController is created to check the latest known location. When it's finished, it cleans up and the location is sent to a database (which is a necessity for this concept).

This all works fine, as long as I check the location with GPS_PROVIDER. However, when I switch it around to NETWORK_PROVIDER, it may check the location once more before dying completely.

I've tried multiple options to prevent this problem, but nothing seems to be working with the update service when I swap that 1 setting.

Here are a few snippets which should be relevant to the service:

UpdateService:

 @Override
public void onCreate() {
    super.onCreate();
    Log.d("Debug", "Created service");
    PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
    IntentFilter filter = new IntentFilter();
    filter.addAction(UPDATE_ACTION);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d("Starting", "Starting Service");

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(UPDATE_ACTION);
    alarmManagerPendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
    alarmManagerPendingIntent.cancel();
    mAlarmManager.cancel(alarmManagerPendingIntent);

    mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                AlarmManager.INTERVAL_FIFTEEN_MINUTES,
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, 
                alarmManagerPendingIntent);

    mLocationController = new LocationController(this, this);

    updateHandler = new Handler();

    return START_STICKY;
}

LocationController:

private LocationListener locationListener = new LocationListener() {
    private boolean didSendLocation;
    public void onLocationChanged(Location location) {
        mLastLocation = location;
        mCallback.locationUpdate(location,false);
        didSendLocation = true;
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Debug", "Status changed: "+status);
        Location _lastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Log.d("Debug", "Last location: "+_lastLocation); 
        if(!didSendLocation)
        {

            mCallback.locationUpdate(_lastLocation,false);
        }
        didSendLocation = false;
    }

    public void onProviderEnabled(String provider) {
        Log.d("Debug", "Provider Enabled");
    }

    public void onProviderDisabled(String provider) {
        Log.d("Debug", "Provider disabled");
    }
};

public LocationController(Context mContext, LocationControllerCallback callback) {
    this.mContext = mContext;
    mCallback = callback;
    Log.d("Debug", "Created LocationController");
    mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
    mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    mCallback.locationUpdate(mLastLocation,true);
}

So, the code works as it is now, but when I swap:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);

with

mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 100, locationListener);

It will no longer update. Any ideas?


Solution

  • I've solved the problem by switching to the Google Play Fused Location provider. A more detailed post around this can be found here: https://stackoverflow.com/a/19282976/3532181

    Sadly the standard network provider just seemed too unreliable when it came to updates, ranging from an update within 8 minutes to hours before a new update was sent to the app.