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...
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
Related
When an Android app is forced closed (System.exit(0)) or it crashes, the service associated with it, running in the background is not destroyed. In simple words, the onDestroy() method of service is not called. Because of this when the android app is restarted, the service starts up again ( its onCreate() is called right away). I want to avoid this.
So, the question is :
How Can I Destroy the background service in such a case as I don't have access to its onDestroy()?
If you are extending a regular plain Service class, then what you say won't happen. I think you are starting a thread for your service. If that's the case, then make sure you call stopService with the right intent to close the running Service. I am assuming your Service is a started service type and not binding type.
I want to write an android service that will run with an app. I want to run this service across multiple activities and have the ability to pass information back to the UI with all activities, not just the activity that starts the service.
From what I understand, I have call startService and not bind it to the activity. Is this correct?
Also, I do not want to start a new instance of the service, I want to use the same instance. If i call startService in each activity, will that use the same, running instance of the service?
startService will start the service only if there's not a previous service running, it won't spawn a second service.
Once you start your service, you can connect to it from any Activity you want, but to pass information back and forth, you need to bind to the service, so you can obtain a pointer to it.
Make sure to read Android's documentation, because the way services work is a little confusing at the beginning.
I have an activity that binds to a service. The service provides the functions to interact with an XMPP server.
Then, the activity launches a second activity that needs to access to the same service (for instance to initiate a voice call).
I was thinking to bind the second activity again to the same service, but it seems like an overkill, since when the second activity starts the service should already exist and should be there until the first activity destroys it on purpose (binding again means creating a new connection and waiting for the bind to happen asynchronously before using the service).
At the moment I'm using startActivityForResult() to launch the second activity and then I wait for the result and I access the service from the first activity, but I want to change the logic (the second activity must interact with the service and then the first one takes care of closing the second activity when a signal is received back from the service).
Any suggestion on how can I pass the service object to the second activity?
binding 2 activities to the same service is no overkill. It is actually the proper way of doing what i understand you are trying to do : access xmpp functions provided by a service from 2 different activities.
by binding the second activity, you will not start the service again, as it has already been started. it will connect to the same service as the first activity.
you could, of course, put all the binding to a single place, like a singleton or the Application class.
You could store a reference to it in the Application and then access it from your second activity.
My application has one service and a set of activities.
Each activity covers a little task (such as ask the user to insert a number, or a text, or to express a preference).
The service starts the activities or a sub set of the activities.
The order in which the service starts the activities changes with day hours.
But every time, the service has to wait the end of an activity (to obtain the activity result) before starts the next activity.
My idea was to use a wait() call in the service between two activities execution.
Each activity uses a sendBroadcast to return the result to a BroadcastReceiver.
The BroadcastReceiver executes the notify() to free the service and forwards the activity result to the service.
Obviously, this idea does not work. The problems are:
1) Starting many activities from a service gives me problem related to the Task each activities belongs to.
2) I don't know how to pass information from the BroadcastReceiver to the Service (in a first time I declared the BroadcastReceiver inside the Service class, but when the service entered the wait() the process remained blocked and BroadcastReceiver never receives).
Please, I accept all kind of suggestions. Maybe a change in the application architecture?
Thanks
The flow is typically one activity triggering the next. You should re-architect so that when you are ready to finish() activity1, you already know what Activity2 is and you start it from Activity1. Perhaps your service should expose a method that your activities can call to get a determination which should be the next activity. It is OK to do so as Services and Activities run in the same process and can call each other.
I have a service running in background. I started it from an Activity, but I want to recover an instance of that service from another activity (in the same app) in order to call one method.
Is it possible?
There is only one instance of the service, at most. You cannot have two instances. Hence, just have the second activity bind to the service ("recovery an instance").