I'm trying to write a simple media player that plays streaming audio using RTSP. I have a GUI-activity and a service that performs the playback. My question is how to best communicate between the activity and the service (e.g. updating the gui based on the player state).
I know that I can bind the service to the activity using onBind(), but if I understand correctly this will stop the service if the activity is killed. I want to continue the playback even if the user exits the activity. Is there any standard or preferred way of dealing with this problem?
I tried to call
consequently and I could create a sticky service and bind to it. Detailed tutorial for Bound Service Example.
There is a method called unbindService that will take a ServiceConnection which you will have created upon calling bindService. This will allow you to disconnect from the service while still leaving it running.
This may pose a problem when you connect to it again, since you probably don't know whether it's running or not when you start the activity again, so you'll have to consider that in your activity code.
Good luck!
If the user backs out, the
onDestroy()
method will be called. This method is to stop any service that is used in the application. So if you want to continue the service even if the user backs out of the application, just eraseonDestroy()
. Hope this help."If you start an android Service with
startService(..)
that Service will remain running until you explicitly invokestopService(..)
. There are two reasons that a service can be run by the system. If someone callsContext.startService()
then the system will retrieve the service (creating it and calling itsonCreate()
method if needed) and then call itsonStartCommand(Intent, int, int)
method with the arguments supplied by the client. The service will at this point continue running untilContext.stopService()
orstopSelf()
is called. Note that multiple calls toContext.startService()
do not nest (though they do result in multiple corresponding calls toonStartCommand()
), so no matter how many times it is started a service will be stopped onceContext.stopService()
orstopSelf()
is called; however, services can use theirstopSelf(int)
method to ensure the service is not stopped until started intents have been processed.Clients can also use
Context.bindService()
to obtain a persistent connection to a service. This likewise creates the service if it is not already running (callingonCreate()
while doing so), but does not callonStartCommand()
. The client will receive theIBinder
object that the service returns from itsonBind(Intent)
method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the Service'sIBinder
). Usually theIBinder
returned is for a complex interface that has been written in AIDL.A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the
Context.BIND_AUTO_CREATE
flag. Once neither of these situations hold, the Service'sonDestroy()
method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning fromonDestroy()
."This is a biased answer, but I wrote a library that may simplify the usage of Android Services, if they run locally in the same process as the app: https://github.com/germnix/acacia
Basically you define an interface annotated with @Service and its implementing class, and the library creates and binds the service, handles the connection and the background worker thread:
You can get an instance of the associated android.app.Service to hide/show persistent notifications, use your own android.app.Service and manually handle threading if you wish.
First of all, 2 thing that we need to understand
Client
it make request to specific server
here
mServiceConn
is instance ofServiceConnection
class(inbuilt) it is actually interface that we need to implement with two (1st for network connected and 2nd network not connected) method to monitor network connection state.Server
Now at client side, how to access all the method of server?
server send response with IBind Object.so IBind object is our handler which access all the method of service by using (.) operator.
now how to call method which lies in service
here
myService
is object andserviceMethode
is method in service. And by this way communication is established between client and server.