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?
source share