Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind service to activity in Android

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?

like image 573
aspartame Avatar asked Dec 16 '09 17:12

aspartame


People also ask

How send data from service to activity in Android?

class MainActivity : AppCompatActivity() { private val sb = object :SerToBroad(){ override fun broadcastResult(connected: String?) { t(connected) } } override fun onCreate(savedInstanceState: Bundle?) { super. onCreate(savedInstanceState) setContentView(R. layout. activity_main) } fun t(connected: String?) { Toast.

How do you create a bound service?

There are three ways of creating bound services: First is by extending the binder class. Second is by using a messenger. Third, is by using AIDL or Android Interface Definition Language.

Can I bind a foreground service?

So in such case, we need to create a Foreground service and can bind our Activity to the Service class. When any client bind to the Service and another client request to bind the same service. It will get the instance of service created previously and our service class will not be restarted or anything.

What is difference between started and bound services in Android?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.


1 Answers

"If you start an android Service with startService(..) that Service will remain running until you explicitly invoke stopService(..). There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(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 (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(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's IBinder). Usually the IBinder 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's onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy()."

like image 54
Schildmeijer Avatar answered Sep 28 '22 14:09

Schildmeijer