0

I want to conserve on battery so I am trying to decide if I should leave LocationClient connected, or connect / disconnect in onStart / onStop like in the official examples. I want to be location aware in my entire app, which means I could potentially connect / disconnect a lot.

theblang
  • 10,215
  • 9
  • 69
  • 120

2 Answers2

0

bothLocationClient continues to get location updates if:

1) is connected

2) updates are requested

If those 2 are true, then it will receive updates in background as well

Sam
  • 1,652
  • 17
  • 25
  • So then I should `connect / disconnect` in `onStart / onStop` then? Will this be harmful to battery performance? – theblang Feb 10 '14 at 16:37
  • Yes, it's harmful on battery performance on old devices. That's up to you whether to connect/disconnect, but I recommend to disconnect when app goes background if you don't need updates in bg. Otherwise, I recommend making a service which deals with location – Sam Feb 10 '14 at 16:43
  • I would like to disconnect when app goes to background, but how could I do that on Android? – theblang Feb 10 '14 at 16:44
  • Call mLocationClient.disconnect(); Should work just fine. Also check out other answer, there're some useful links – Sam Feb 10 '14 at 16:46
  • Sorry, I meant where would I put the `disconnect` call? I can't use it in any of the Activity lifecycle functions if I want my entire location to be location aware because then I would connect / disconnect every time I changed Activities. – theblang Feb 10 '14 at 16:51
  • Use service and Intent-BroadcastReceiver system then – Sam Feb 10 '14 at 16:52
0

Is your location client declared in an Activity or Service? My experience shows that if it is in an Activity, the location updates will not be received because your Activity is in sleep when not in foreground (i.e. user not using it).

To continuously receive updates, you need a Service in Android.

This thread shows you exactly how to do that.

Community
  • 1
  • 1
user1406716
  • 9,565
  • 22
  • 96
  • 151
  • It is declared in `Activity`. I want to conserve on battery by not updating location when the app is in the background. I `edited` my question to more accurately reflect that desire. – theblang Feb 10 '14 at 16:36
  • if you activity is not killed by the OS (and just paused by the user), it will continue to get updates. You can stop this by adding 'removeLocationUpdates()' in the onPause method. Dont forget to add 'requestLocationUpdates()' in the onResume method. If this helps, I will add it to my answer. – user1406716 Feb 10 '14 at 16:58
  • I just did a test. The `LocationClient` that I declared in my Activity does in fact continue to retrieve location updates. I assume this because the `LocationListener` callback that I declared continues to run even if I move my app to the background. – theblang Feb 10 '14 at 17:03
  • Yes, that is expected because Android did not really kill your app. Android OS will kill your app only if it is low on memory or if you call the "finish()" function in your onPause ... that is an option, but then you should not forget to call requestLocationUpdates() in your 'onCreate()' – user1406716 Feb 10 '14 at 17:05
  • If you have multiple activities requesting location, you should really do this in a Bounded Service: http://developer.android.com/guide/components/bound-services.html – user1406716 Feb 10 '14 at 17:06
  • What about just `removing / requesting` in `onStart / onStop` like you mentioned? That shouldn't effect battery right? – theblang Feb 10 '14 at 17:07
  • Yes that will work fine, you will just need to remember to do it in all the Activities that you want to implement this behavior in. – user1406716 Feb 10 '14 at 17:07