1

I'm using a LocationListener to obtain locations. The locations must continue being registered even when my app has no visible activities/fragments (such as user pressing Home button).

What is the difference in these designs:

1) Registering the LocationListener in an activity. Unregister it in onDestroy.

2) Registering the LocationListener in a service. User must stop this service from an activity.

Will my listener be called even if the activity is not visible?

What thread will actually call my listener? My app main thread or a system thread?

If it is called in both (1) and (2), what are the pros and cons?

Better design proposals are very welcome.

[Edit 1 start]

I've found the answer for my question about which thread will call the listener. Depending on the overload of requestLocationUpdates used, it will either be the supplied Looper thread or the calling thread (which must be a Looper thread).

Source: 1

"If a LocationListener is used but with no Looper specified then the calling thread must already be a Looper thread such as the main thread of the calling Activity. If a Looper is specified with a LocationListener then callbacks are made on the supplied Looper thread."

This basically means, if I want to offload the main thread, I need to either supply a Looper thread to requestLocationUpdates or create a worker thread when the listener is called. And I still don't see the difference in doing this from a service or from an activity.

[Edit 1 end]

jgreen81
  • 705
  • 1
  • 6
  • 14
  • See, if you want to get the location time to time, you can implement it on Service with Alaram Manager. But remember it will consume battery power more. It is the pros & cons of point#1. For point#2, the solution doen't fulfil your requirement. – sUndeep May 09 '14 at 08:35
  • To clarify. I need the location as often as possible, say, like Google MyTracks or Endomondo sports trainer or something. – jgreen81 May 09 '14 at 09:52
  • Go for #2 then with Alarm Manager – sUndeep May 09 '14 at 10:08
  • I'm not following. My goal is to receive locations as often as possible. I do that by registering a listener and then I get an "onLocationChanged" call. I don't want to be doing anything at regular intervals so what could I use an AlarmManager for? – jgreen81 May 09 '14 at 11:45
  • Ohh. then please ignore my suggestion to use Alaram Manager. Rest #2 is fine for you to implement – sUndeep May 09 '14 at 12:53
  • No problem. Thanks for trying to help. If I really wanted locations only at regular intervals, there's a "minTime" parameter to registerLocationUpdates method. – jgreen81 May 09 '14 at 17:46

1 Answers1

0

Well, lets back to the base of the question Registering the LocationListener Activity vs Service -if i understand well-

the activity will -at some point- get destroyed, mostly when its in background but the service will live longer, as long as nothing critical occurs and the OS shuts it down. so basically that is the difference

here is 2 Ref talking about this, Android: Service stops when Activity is closed Android: Create service that runs when application stops

and from ur service, you can store the location or send it to server ...

Community
  • 1
  • 1
Yazan
  • 6,074
  • 1
  • 19
  • 33
  • But even if all my activities, fragments, services and other app components are destroyed, my app main thread still runs and my listener is still registered with the LocationManager. Correct me if I'm wrong. But this is basically why I don't see the difference. Please notice that there might not be a difference. I'm only wondering if there is. My question could have been more clear. – jgreen81 May 09 '14 at 18:43
  • well iam not sure, how u gonna get the main thread as Looper? because as i understand, the UI thread is the main thread, so when u register from activity, with null as looper, its on the UI thread which is the main thread... – Yazan May 09 '14 at 19:04
  • Yup. So if I don't supply anything and just register my listener from my main thread, the main thread will execute the calls on the listener. – jgreen81 May 09 '14 at 19:13
  • yes but if the main thread is closed, -i think- the service will NOT, so you got advantage here... – Yazan May 09 '14 at 19:21