Issue
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 :-
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 ?
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().
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...
Solution
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
Answered By - baboo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.