Epixoft/android-protips-location

Only use passive updates when in background

Opened this issue · 0 comments

I found that both Passive and Active updates were being done when the app was 
in the foreground. I had changed the accuracy to COARSE

My solution was to disable the updates for the passive listener in onCreate() 
and only enable in onPause() when not exiting the app:

in onCreate
        Intent passiveIntent = new Intent(this, PassiveLocationChangedReceiver.class);
        locationListenerPassivePendingIntent = PendingIntent.getBroadcast(this, 0, passiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        locationManager.removeUpdates(locationListenerPassivePendingIntent);

in disableLocationUpdates
        if (AppConstants.DISABLE_PASSIVE_LOCATION_WHEN_USER_EXIT && isFinishing())
            locationManager.removeUpdates(locationListenerPassivePendingIntent);
        else
            locationUpdateRequester.requestPassiveLocationUpdates(AppConstants.PASSIVE_MAX_TIME, AppConstants.PASSIVE_MAX_DISTANCE, locationListenerPassivePendingIntent);

Great article and excellent code. Thanks for this!

Original issue reported on code.google.com by bjw...@gmail.com on 13 Jul 2011 at 4:07