othomas / android-protips-location

Automatically exported from code.google.com/p/android-protips-location
0 stars 0 forks source link

Only use passive updates when in background #5

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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