Communication from application to service

I want to communicate with my Android application on my Android service. I have two options, but I don’t know what to choose:

  • Register a service using the application
  • Use LocalBinder to connect from the application to the service.

Solution 1

Application:

public class MyApplication extends Application { MyService myService; public void setMyService(MyService myService) { this.myService = myService; } public void testCallService(){ myService.sendResponseApdu("test".getBytes()); } } 

and 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 link to the service. Service is a local service. (non remote service) Will this approach work under any circumstances?

Decision 2

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

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

+5
source share
6 answers

According to the official documentation for Android, the service is designed to perform lengthy operations in the background, and if there is significant interaction between the service and the actions, they recommend using related services. The reason for using related services is that Binding has a rich interface for communication.

fooobar.com/questions/1202656 / ...

I was working on a similar application when I selected related services for the same reason as activity communication in order to service through the interface and send events from service to activity using Localbroadcastreceiver.

+2
source

The second approach seems to me better, because the connection of the first is more complex.

0
source

The use of binders, or to say AIDLS in the case of interaction between processes, as always preferred, using it by reference. In case of leaks or dead links, your application will work.

0
source

If you want to use full isolation, you can use broadcast receivers

0
source

If your service is local to your application, the best way (easier and cleaner) is to use LocalBroadcastManager http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html This way you can transfer any objects between actions / snippets / services without worrying about the security and complexity of your code. This helps you keep your code lean and maintenance quite simple.

Hope this helps.

0
source

It provides an easy way to communicate with the service:

1) Implement your own Android soundtrack

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

3) Sending / receiving messages by the service

4) Send / receive activity messages

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

0
source

Source: https://habr.com/ru/post/1202655/


All Articles