public final class LocalBroadcastManager


Helper to register for and send broadcasts of Intents to local objects within your process. This has a number of advantages over sending global broadcasts with sendBroadcast:

  • You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system.

Summary

Public methods

static @NonNull LocalBroadcastManager
void
registerReceiver(
    @NonNull BroadcastReceiver receiver,
    @NonNull IntentFilter filter
)

Register a receive for any local broadcasts that match the given IntentFilter.

boolean

Broadcast the given intent to all interested BroadcastReceivers.

void

Like sendBroadcast, but if there are any receivers for the Intent this function will block and immediately dispatch them before returning.

void

Unregister a previously registered BroadcastReceiver.

Public methods

getInstance

public static @NonNull LocalBroadcastManager getInstance(@NonNull Context context)

registerReceiver

public void registerReceiver(
    @NonNull BroadcastReceiver receiver,
    @NonNull IntentFilter filter
)

Register a receive for any local broadcasts that match the given IntentFilter.

Parameters
@NonNull BroadcastReceiver receiver

The BroadcastReceiver to handle the broadcast.

@NonNull IntentFilter filter

Selects the Intent broadcasts to be received.

sendBroadcast

public boolean sendBroadcast(@NonNull Intent intent)

Broadcast the given intent to all interested BroadcastReceivers. This call is asynchronous; it returns immediately, and you will continue executing while the receivers are run.

Parameters
@NonNull Intent intent

The Intent to broadcast; all receivers matching this Intent will receive the broadcast.

Returns
boolean

Returns true if the intent has been scheduled for delivery to one or more broadcast receivers. (Note that delivery may not ultimately take place if one of those receivers is unregistered before it is dispatched.)

See also
registerReceiver

sendBroadcastSync

public void sendBroadcastSync(@NonNull Intent intent)

Like sendBroadcast, but if there are any receivers for the Intent this function will block and immediately dispatch them before returning.

unregisterReceiver

public void unregisterReceiver(@NonNull BroadcastReceiver receiver)

Unregister a previously registered BroadcastReceiver. All filters that have been registered for this BroadcastReceiver will be removed.

Parameters
@NonNull BroadcastReceiver receiver

The BroadcastReceiver to unregister.

See also
registerReceiver