1

In my App. I have extended Application class, say MyApp and I have few activities say A,B,C,D where A is the default Activity or root Activity. I want to start a Service (say MyService) inside OnCreate of the MyApp and stop it when User logs out from my Application,

However I need a strong way to communicate between my Activities (A,B,C,D), MyApp and MyService. Now Application is launched second time from the Memory. OnCreate of MyApp will not be called so I would like to start the Service from OnClick of the SignIn button which is inside activity A. Now I have a number of questions :-

  1. I have tried both startService and bindService. Problem with startService is I don't get back the Service reference for future use. If I use bindService will that make my Service visible through out the Application life cycle till I explicitly stop it. Which way is applicable in my case ?

  2. What is the best way to implement communication between Service and MyApp / Activities and vice a verse ? By Communication I mean two type of communication, 1. that needs UI thread i.e. start some activity, show some dialog etc. 2. Something like myService.doSomehing().

  3. The Service has a thread which does network communication which should not be closed during the life time of the Application. START_STICKY will work for me or I need to make extra arrangements for that.

This app is supposed to run for GingerBread+ devices...

Amit
  • 13,134
  • 17
  • 77
  • 148

1 Answers1

1

Bind the service inside your extended app oncreate and create a public method (getBinding) which returns that binding object ... In your root/default activity 'A' oncreate via getBinding method on casted getApplication object retrieve .. unbind when all activities close (keep counter with each getBinding method)

check this Cleanly binding/unbinding to a Service in an Application

so for 1) Bind service 2) the binding obj 3) START STICKY would be enough

Community
  • 1
  • 1
baboo
  • 1,983
  • 17
  • 23
  • thanks @baboo for the quick answer, What if I do binding inside OnCreate of the App... and than other activities can MyApp's public method getMyService() in which I will return the service object recieved inside onServiceConnected of the ServiceConnection. Will that work form me ? – Amit Feb 06 '13 at 07:57
  • suppose I want to run some code in UI thread inside Service... What is the best way to do that ? – Amit Feb 06 '13 at 08:00
  • yup it will work.. and further depth on this mechanism check this http://stackoverflow.com/questions/3141632/android-service-interacting-with-multiple-activities – baboo Feb 06 '13 at 08:07
  • for Ui thread http://stackoverflow.com/questions/6369287/accessing-ui-thread-handler-from-a-service – baboo Feb 06 '13 at 08:10
  • thanks... I really wonder how you found those threads. I have tried so many search strings in last two days but couldn't get any of them... Thanks once again... – Amit Feb 06 '13 at 08:18
  • 1
    hey no probs. did this research while implementing this mechanism in my own app. do check this as well :) .. http://www.ozdroid.com/#!BLOG/2010/12/19/How_to_make_a_local_Service_and_bind_to_it_in_Android .. it helps in handling memory leak issues with bound services – baboo Feb 06 '13 at 13:10