Local Broadcast, less overhead and secure in Android

Basics of Broadcast

Register Broadcast

Receive Broadcasts

Problem with global broadcast

What is LocalBroadcastManager?

Implementation

compile ‘com.android.support:support-v4:23.4.0’
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
// Create intent with actionIntent localIntent = new Intent(“CUSTOM_ACTION”);// Send local broadcastlocalBroadcastManager.sendBroadcast(localIntent);
private BroadcastReceiver listener = new BroadcastReceiver() {@Override    public void onReceive( Context context, Intent intent ) {        String data = intent.getStringExtra(“DATA”);        Log.d( “Received data : “, data);    }};
localBroadcastManager.unregisterReceiver(myBroadcastReceiver);

How to secure broadcasts

Restrict your app to receive broadcast

<receiver android:name=”.MyBroadcastReceiver”          android:permission=”android.permission.SEND_SMS”>    <intent-filter>    <action android:name=”android.intent.action.AIRPLANE_MODE”/>    </intent-filter></receiver>

Control receiver of your broadcast

sendBroadcast(new Intent(“com.example.NOTIFY”), Manifest.permission.SEND_SMS);

Conclusion

Senior Software Engineer at Mastercard. Dreamer and Achiever..

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store