Link service to activity on Android

I am trying to write a simple media player that plays streaming audio using RTSP. I have a GUI activity and a service that performs playback. My question is how best to communicate between activity and service (for example, update gui based on player state).

I know that I can bind a service to activity using onBind (), but if I understand correctly, it will stop the service if the action is killed. I want to continue playing, even if the user exits this operation. Is there any standard or preferred way to solve this problem?

+75
android service
Dec 16 '09 at 17:28
source share
6 answers

"If you start the android service using startService(..) , this service will work until you explicitly call stopService(..) . There are two reasons why the system can manage the service. If someone calls Context.startService() , then the system will receive the service (creating it and calling its onCreate() method, if necessary), and then calling its onStartCommand(Intent, int, int) method with the arguments provided by the client. The service will continue to work until until Context.stopService() or stopSelf() called. Note that several calls to Context.startService() are not nested (although they lead to several corresponding calls to onStartCommand() ), so no matter how many times it starts, the service will be stopped after calling Context.stopService() or stopSelf() ; however, services can use their stopSelf(int) method stopSelf(int) to ensure that the service is not stopped until the started intentions are processed.

Clients can also use Context.bindService() to get a permanent connection to the service. This also creates the service if it is not already running (calling onCreate() at the same time), but does not call onStartCommand() . The client will receive an IBinder object that the service returns from its onBind(Intent) method, allowing the client to make callbacks to the service. The service will work until the connection is established (regardless of whether the client keeps a link to the IBinder Service). The usually returned IBinder is a sophisticated interface written in AIDL.

A service can be started and associated with it. In this case, the system will keep the service running as long as it is running or there is one or more connections to it with the Context.BIND_AUTO_CREATE flag. When none of these situations occurs, the Service onDestroy() method is called and the service actually terminates. All cleaning (stopping threads, unregistered receivers) should be completed after returning from onDestroy() .

+135
Dec 16 '09 at 18:27
source share

First of all, we need to understand what is needed

Client

  • it requests a specific server

     bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);` 

here mServiceConn is an instance of the ServiceConnection class (built-in), it is actually an interface that we need to implement using two (1st for a network-connected and 2nd network-not connected) method for monitoring the status of a network connection.

Server

  • It processes the client’s request and makes a copy of its own, which is private only for clients who send the request, and this server replica runs in different threads.

Now on the client side, how to access the entire server method?

  • Response to sending a server with IBind Object.so The IBind object is our handler, which accesses the entire service method using the (.) Operator.

     MyService myService; public ServiceConnection myConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { Log.d("ServiceConnection","connected"); myService = binder; } //binder comes from server to communicate with method of public void onServiceDisconnected(ComponentName className) { Log.d("ServiceConnection","disconnected"); myService = null; } } 

now how to call a method that is in the service

 myservice.serviceMethod(); 

here myService is an object, and serviceMethode is a method in maintenance. And thus the connection is established between the client and the server.

+20
Aug 22 '14 at 7:24
source share

I tried to call

 startService(oIntent); bindService(oIntent, mConnection, Context.BIND_AUTO_CREATE); 

therefore, I could create a sticky service and tie it. A detailed tutorial for Example Bound Service .

+8
May 09 '11 at 16:08
source share

There is an unbindService method that will accept the ServiceConnection that you created when you called bindService. This will allow you to disconnect from the service, leaving it operational.

This can create a problem when you reconnect to it, since you probably don’t know if it works or not when you restart it, so you will have to consider this in your activity code.

Good luck

+5
Dec 16 '09 at 17:38
source share

This is a biased answer, but I wrote a library that can simplify the use of Android services if they run locally in the same process as the application: https://github.com/germnix/acacia

Basically, you define an interface annotated with @Service and its implementing class, and the library creates and binds this service, processes the connection and the background workflow:

 @Service(ServiceImpl.class) public interface MyService { void doProcessing(Foo aComplexParam); } public class ServiceImpl implements MyService { // your implementation } MyService service = Acacia.createService(context, MyService.class); service.doProcessing(foo); <application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> ... <service android:name="com.gmr.acacia.AcaciaService"/> ... </application> 

You can get an instance of the android.app.Service associated with it to hide / show persistent notifications, use your own android.app.Service and manually process the streams if you want.

-one
May 02 '15 at 16:42
source share

If the user onDestroy() back, the onDestroy() method will be called. This method should stop any service that is used in the application. Therefore, if you want to continue the service, even if the user backs away from the application, simply delete onDestroy() . I hope for this help.

-four
Apr 29 2018-12-12T00:
source share



All Articles