0

There are several ways to connect to Service to Activity. I am only interested in local service and my LocalService will stand there untill user stops it(which also means end of app). I might know things wrong, if so please correct me.

On the reference page, it is stated that in order to use methods of local service directly, we should use ServiceConnection. After binding, we can have a reference to LocalService class, and we can use methods of this LocalService directly. AFAIK the methods we call using this reference run on main thread with relevant Activity.

The thing that confuses me, what if I use skeleton structure and access LocalService's methods by directly its static reference (ie. by LocalService.getInstance()). Well, I have already used it and did not face any problem, but still I am not sure which one is better, and why.

Thanks in advance. I might add additional info if requested.

edit: In my previously mentioned solution, no activity is keeping a reference to the LocalService.

It is used to

  1. start some LongRunningAsyncTasks(which are all halted and reference-nullified before service stop),
  2. update the app Notification,
  3. get getFilesDir(),
  4. to keep an enum value (whose reference is not kept elsewhere, it is just used for comparison) in order to access from everywhere(not worthy of using SharedPreferences).
  5. show some toasts
guness
  • 6,336
  • 7
  • 59
  • 88

2 Answers2

1

aware of static references of activities and services because they can be a reason of memory leak. if you don't want your service run in main process, then extract it into another process and work with service connection.

If you don't need any feedback from service, then just don't use connection and simply use startService() with several commands which will be executed in onStartCommand() method of the service.

If you need feedback from service, but not frequently, then use startService() and feedback from service with sendBroadcast() or through Handler class.

If you need feedback frequently (for example update slider of media player), then it's better to use service connection.

Remember that your service can be killed anytime without executing method onDestroy() and without any notification, that's why keeping static reference is not good idea.

Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • I do not need of extra process for my service(I am only interested in LocalService). I do not keep a reference to the service, (always using by it getInstance();) and that instance is being nulled on service stop. Does this change anything? – guness Dec 10 '13 at 12:37
  • you deliver instance in your activity, and when your service is going to die, reference to this instance still remains - guess where? – Autocrab Dec 10 '13 at 12:39
  • I see nowhere except some LongRunningTasks which are all terminated before even service stops. And their references are also nullified. Am I doing something perfect rather than usual? Or we could not be able to perfectly understand each other(I prefer second one due my weak english/android). – guness Dec 10 '13 at 12:48
  • i guess you are nulling instance in onDestroy() method which is not called everytime. Your service can be simply killed by OS and won't call onDestroy() method. Therefore reference will remain. If you want to work with static instance then at least use class WeakReference. – Autocrab Dec 10 '13 at 12:58
  • Thanks for WeakReference offer, besides I am nulling instance just before selfStopping. – guness Dec 10 '13 at 13:17
  • Remember that your service can be killed in low battery/memory situations. That's why you can't rely on nulling instance. – Autocrab Dec 10 '13 at 13:51
1

It appears from what you are saying that you probably don't need a Service at all. Looks like you are not doing any long-standing task in your LocalService. If that's the case, you can as well use AsyncTask or Handlers and be done. The motivation to use a Service (Local or otherwise) is to do some long standing task inside it and not stall the main UI thread. If your tasks are not gonna take up too much time, then you don't need a Service.

The Service does run on the main thread by default. Unless it's an IntentService where a worker thread is created for you automatically and all tasks are queued and handled one at a time in this worker thread. Otherwise, it's your responsibility to create a separate thread for your service tasks.

So, first analyze if you really need a Service. If your task can quickly get executed, then don't bother having a Service even.

Hope that helps.

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • Least of my LongRunningAsyncTasks needs 4-5 secs, some needs 5-10 minutes. I guess I need some Service didn't I? – guness Dec 10 '13 at 13:24
  • Yes, anything more than 4 seconds, advisable to have it run on another thread (Service or AsyncTasks or Handlers). – VJ Vélan Solutions Dec 10 '13 at 13:27
  • So, your question mainly is why can't one just get away using static references to the Service object, is it? – VJ Vélan Solutions Dec 10 '13 at 13:30
  • I know some about other cases(RemoteService etc.) but in my situation, yes you can say that. – guness Dec 10 '13 at 13:32
  • @bluebrain Autocrab is on the right track. The process in which your application runs can be killed/terminated at any time when it's in the background by the system. So, having static references can then lead to memory leaks. – VJ Vélan Solutions Dec 10 '13 at 14:16