How to use LocalBroadcastManager in Android?
Advantages of using LocalBroadcast Receiver over Broadcast Reciever?
- Secure Out: Your broadcasted data remains inside the App. So other apps can not receive your data
- Secure In: Your app will not receive Data from other apps Broadcast.
- Drains less battery: It is a more efficient way to send/receive data locally.
How to change your global Broadcast to LocalBroadcast?
LocalBroadcastReceiver is the same as BroadcastReceiver in Android. So if you know how to use BroadcastReceiver then you have got this. The code is almost similar.
1) Create Instance
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
2) For registering BroadcastReceiver
registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
localBroadcastManager.registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
3) For sending a broadcast message
4) For unregistering BroadcastReceiver
And that's it.