Now Activity can connect to services using one of three ways:
I think that BroadcastReceivers is the easiest way to communicate but I'm wondering why and when to use other ways? or in other words in which cases messengers or AIDL will be the best practice to use than broadcastreceivers?
I mostly tend to use LocalBroadcasts
. They essentially are like real broadcasts, but only visible to your application. First you have to create a BroadcastReceiver
like you would with a normal broadcast:
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.SOME_ACTION.equals(action)) {
// Do your work
}
}
};
You can then register and unregister the BroadcastReceiver
like this:
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(Intent.SOME_ACTION);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.unregisterReceiver(broadcastReceiver);
}
And finally you can send a broadcast from your Service
or anywhere else in your application like this:
Intent intent = new Intent(Intent.SOME_ACTION);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.sendBroadcast(intent);
You can use BroadcastReceiver
when you want the communication between Service
and Activity
in your application.
Messenger
's and AIDL
's are mainly used when your application needs to communicate to other processes(IPC). In this case your interface should have a Service
which defines a Handler
that responds to different types of Message
objects.
Now the difference between Messenger
and AIDL
is pretty simple. When you use Messenger
, it queues all requests into a single thread. So your Service
doesn't have to be thread safe. If, you want your Service
to handle multiple requests simultaneously, then you can use AIDL
directly. In this case, your Service
must be capable of multi-threading and be built thread-safe. In fact Messenger
is implemented on the top of AIDL
.
For better understanding look at Bound Services
You should also check the answer from BroadcastReceiver or Messenger via Handler
Xaver Kapeller's answer is indeed a good one. However, it has a (major) drawback: broadcasts can be missed.
When the user navigates away from the Activity
(for example by showing the recent apps), you unregister your BroadcastReceiver
s. If the broadcast is sent at that moment, your Activity
does not receive it. When navigating back to your Activity
, it might be in an invalid state.
Using a ResultReceiver
, you still receive the result in this case. Furthermore, a ResultReceiver
is parcelable
, so you can save and restore it in the lifecycle events.
It causes some overhead though, which is why I've created this experimental library to ease it up a little. For now it only uses IntentService
s, but it should be easy enough to extend.
If your service is used only by the local application
and does not need to work across processes, then you can implement your own Binder class
that provides your client direct access to public methods in the service
.
Note: This works only if the client and service are in the same application and process`, which is most common.
Extending the Binder class http://developer.android.com/guide/components/bound-services.html#Binder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With