Communicate from Application to Service

2019-05-01 07:45发布

问题:

I want to communicate from my Android Application to my Android Service. I've two options but I don't know which to choose:

  1. Register the service with the application
  2. Use the LocalBinder to connect from the Application to the service.

Solution 1

The application:

public class MyApplication extends Application {

    MyService myService;

    public void setMyService(MyService myService) {
        this.myService = myService;
    }

    public void testCallService(){
        myService.sendResponseApdu("test".getBytes());
    }
}

and the service:

public class MyService extends HostApduService {

    @Override
    public void onCreate() {
        super.onCreate();
        ((MyApplication)getApplication()).setMyService(this);
    }

    @Override
    public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
        return new byte[0];
    }

    @Override
    public void onDeactivated(int reason) {

    }
}

To call the service the application uses the reference to the service. The Service is a local service. (not a remote service) Will this approach work in all circumstances?

Solution 2

Use the LocalService approach with a ServiceConnection to bind to the service conform the example on http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

Solution 2 will work. Will example 1 work too? What are the (dis)advantages of solution 1 compared to solution 2?

回答1:

As per official android documentation, service are meant to perform long running operations in background and if there is significant amount of interaction between service and activities, they recommend to use the bound services. The reason for using bound services is that Binding has advantage of rich interface for communication.

https://stackoverflow.com/a/5066187/2839624

I was working on a similar application where I chose the bound services for the same reason with communication from activity to service via interface and communicating events from service to activity using Localbroadcastreceiver.



回答2:

The second approach seems best to me because the coupling of the first one is tigher.



回答3:

Using binders, or say AIDLS in case of inter process communications as always preferred then by using it via a reference, In case of leakes or dead references your app will go boom.



回答4:

If you want full decoupling use can use broadcast receivers



回答5:

If your service is local to your app then the best (simpler and cleaner) way to do it is using a LocalBroadcastManager http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html That way you can pass any kind of objects between activities/fragments/services without having to worry for security nor the complexity of your code. It helps you keep your code lean and maintenance is quite easier.

Hope it helps.



回答6:

It provides an easy way to communicate with service:

1)Implement your own Android background Service

2)Start and stop the service from an Activity, i.e. the UI

3)Send/receive messages by the Service

4)Send/receive messages by the Activity

follwing link may help you http://blog.philippheckel.com/2012/06/10/android-example-communication-between-activity-and-service-using-messaging/