0

I have location updates set on my app like such

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 100, this);

Which has been working fine. But my issue is since the code is in OnResume it notifies you and your phone beeps ('Finding location') whenever bouncing back to the activity (multi-activity app so quite often) which is annoying

So my question is: Is it safe to remove location updates in OnDestroy? and if not where would I put them?

I know OnDestroy isn't called all the time. If it isn't will it never stop updating the users location and wasting battery?

I don't want to have it keep annoying users so I made updates not get destroyed on pause. That way if you go to a new activity it won't re-notify you. Thanks

@Override
public void onDestroy() {
    if (locationManager != null) {
        locationManager.removeUpdates(this);
    }
}
Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49
George
  • 485
  • 1
  • 5
  • 13
  • alternatively, why not just stop updates when moving to different activities ? set your location manager to null when moving around and recreate in onResume ? – a_local_nobody Jul 12 '19 at 06:46
  • @pieter That's how it is now, It's just with Verizon phones or so whenever you do that it does 1 BEEP for finding and a second BEEP for found on location. Since you go in and out of activities rather fast at times it can get really annoying - yea tested it you can't set listeners to null – George Jul 12 '19 at 06:48
  • I see, im trying to think outside of the box instead of relying on lifecyles :) maybe you can try caching the last retrieved location with a time, and check when the last time was you retrieved the location ? – a_local_nobody Jul 12 '19 at 06:49
  • @pieter Yeah that's probably what I'm going to have to do unless someone tells me that the location updates stop on their own somehow through Android magic. Besides that can't really find a way to tell when the app is exited – George Jul 12 '19 at 06:53
  • might be worth having a look at https://stackoverflow.com/questions/18038399/how-to-check-if-activity-is-in-foreground-or-in-visible-background – a_local_nobody Jul 12 '19 at 06:55
  • What beeps? Is it a notification? If so, instead of moving locationManager maybe you should just disable notifications. You only need them if you want your service to run in foreground. – MarkWalczak Jul 12 '19 at 07:02
  • @a_local_nobody thanks, might be able to just listen for when any activity stops in the application and turn off updates then I guess – George Jul 12 '19 at 07:03
  • @MarkWalczak It's a system notification that might be restricted to Verzion I think. Can't find how to disable it. It beeps when finding a location and when a location is found – George Jul 12 '19 at 07:04
  • If your aplication needs location updates, I would rather find that notification class then mess around with LocationManager. – MarkWalczak Jul 12 '19 at 07:07
  • @MarkWalczak it's a system thing so it's out of my control sadly – George Jul 12 '19 at 07:08
  • I get it. So maybe a "refresh location" button? – MarkWalczak Jul 12 '19 at 07:18
  • @MarkWalczak Yea I'm thinking about just doing that since location isn't that important. It looks like using Network instead of GPS stops it from happening too so I might try that as well. Thanks – George Jul 12 '19 at 07:24

0 Answers0