How to Use Localbroadcastmanager

Android: How to use LocalBroadcastManager

I can only answer about LocalBroadcastManager.

  1. Add support library to your SDK and then to your Android project.
  2. Obviously, import the support library in your java class, etc.
  3. Declare your LocalBroadcastManager and instance it. Also, you can declare it static and use it throughout your (entire) app. So you don't have to instance a new one for every activity, etc.

    public static LocalBroadcastManager mBroadcaster;
    mBroadcaster = LocalBroadcastManager.getInstance(yourAppContextHere);
  4. In every activity, service, etc, register and unregister a Receiver according to each's lifecycle. Each receiver can have different filters. For example in onResume (or onCreate) and onPause (or onDestroy)::

    IntentFilter mFilter = new IntentFilter(MY_ACTION_1);
    mFilter.addAction(MY_ACTION_2);

    mBroadcaster.registerReceiver(localBluetoothReceiver, mFilter);
    mBroadcaster.unregisterReceiver(localBluetoothReceiver);

    And, finally, sending broadcasts and receiving with the receiver:

    Intent sendCmdIntent = new Intent("your.package.name.your.action.name");
    sendCmdIntent.putExtra(key, value);
    mBroadcaster.sendBroadcast(sendCmdIntent);


    private BroadcastReceiver localBluetoothReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Do whatever depending on the action and on the extra stuff you put in the intent.

All this is quoted from memory, feel free to edit it!

How to use Xamarin Android LocalBroadcastManager

There are two issues with this code. First, you should use be registering the Receiver with the LocalBroadcastManager:

 _dirtyMessageReceiver = new DirtyBroadcastReceiver();
RegisterReceiver(_dirtyMessageReceiver, new IntentFilter("dirty"));

Should be

 _dirtyMessageReceiver = new DirtyBroadcastReceiver();
LocalBroadcastManager.GetInstance(this).RegisterReceiver(_dirtyMessageReceiver, new IntentFilter("dirty"));

Secondly, the Unregistering of the Receiver should be one against the LocalBroadcastManager as well:

UnregisterReceiver(_dirtyMessageReceiver);

becomes

LocalBroadcastManager.GetInstance(this).UnregisterReceiver(_dirtyMessageReceiver);

Does LocalBroadcastManager only work in the one Activity?

If you want to receive the LocalBroadcast in various different activities, you will have to register the receiver in all those receiving activities. The Sender activity sends/broadcasts notifications and the receiver activity that watches for notifications has to register the Broadcast Receiver for receiving the notifications.

Android using LocalBroadcastManager inside android Service

To get an outerScope this inside an annoymous method / inner class of any java class you can either call WebSocketService.this

LocalBroadcastManager.getInstance(WebSocketService.this).sendBroadcast(broadcastIntent);

or declare a private helper method for the same

private WebSocketService getOuterWebSocketService() {
return this;
}

Now you can use this method inside onMessage as below

@Override
public void onMessage(String message) {
Log.i(TAG, "Websocket Received: " + message);
Intent broadcastIntent = new Intent("custom-event");
broadcastIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(getOuterWebSocketService()).sendBroadcast(broadcastIntent);
}

Use LocalBroadcastManager to send PendingResult reference (for ordered broadcasts)

I made a Parcelable wrapper for PendingResult as such:

public class PendingResultWrapper implements Parcelable {

private BroadcastReceiver.PendingResult pendingResult; //The only thing we care about in this class!

public PendingResultWrapper(BroadcastReceiver.PendingResult pendingResult) {
this.pendingResult = pendingResult;
}

protected PendingResultWrapper(Parcel in) {
}

public static final Creator<PendingResultWrapper> CREATOR = new Creator<PendingResultWrapper>() {
@Override
public PendingResultWrapper createFromParcel(Parcel in) {
return new PendingResultWrapper(in);
}

@Override
public PendingResultWrapper[] newArray(int size) {
return new PendingResultWrapper[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
}

public BroadcastReceiver.PendingResult getPendingResult() {
return pendingResult;
}

}

It can be strapped to an Intent and broadcasted by a LocalBroadcastManager as an Object reference because local broadcasts are never really serialized, unlike inter-process broadcasts.

It works!



Related Topics



Leave a reply



Submit