Android: How to use LocalBroadcastManager
I can only answer about LocalBroadcastManager.
- Add support library to your SDK and then to your Android project.
- Obviously, import the support library in your java class, etc.
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);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
Apk Signing Error:Failed to Read Key from Keystore
Retrieve Stored Image from Firebase Storage
Get Height and Width of a Layout Programmatically
How to Change the Background Color of the Options Menu
How to Remove Background Resource of Button
Android Studio Inline Compiler Showing Red Errors, But Compilation With Gradle Works Fine
How to Show Only Specific Part of Website in Android Webview
How to Give Pause or Gap Between Words in Tts in Android
Android Navigationview: Reduce Space Between Icon and Text and 'Itembackground' Not Working
Flutter Keyboard Makes Textfield Hide
How to Add the Json Response into Spinner in Android Using Retrofit
How to Post Raw Whole Json in the Body of a Retrofit Request
How to Load New Values of Json After Every 10 Seconds
Not Opening Specific Activity on Notification Click When the App Is in Background/Not Running
Android Compress Video Before Upload to Server
How to Make a Background 20% Transparent on Android