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
- start some
LongRunningAsyncTasks
(which are all halted and reference-nullified before service stop), - update the app
Notification
, - get
getFilesDir()
, - 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).
- show some toasts