broadcast receiver example in android with local broadcast

Broadcast Receiver Example In Android

Broadcast Receiver is a component that responds to a broadcast message sent by any other component. The mechanism to send message is through broadcast intents. Broadcasted message by clients can be received by many receivers. Receivers of the broadcast message are inherited from BroadcastReceiver class.

Broadcast receiver can be registered in AndroidManifest as well as can be created runtime.

    public final String INTENT_ACTION = "com.codeexa.broadcast.ACTION";

Sending Broadcast:

private void sendBroadcastMessage(Context context){
        Intent broadcastMsg = new Intent(INTENT_ACTION);
        broadcastMsg.putExtra("message", "Sending data through broadcast");
        context.sendBroadcast(broadcastMsg);
}

Action is an identifier to identify the broadcast. You can change it as per your need. Receiver are also set for receiving pending intents set by Alarm Manager.

Receiving Broadcast:

Create our receiver class that will receive the broadcast with action "com.codeexa.broadcast.ACTION"

Go to File -> New -> Other -> BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String msgReceived = intent.getStringExtra("message");
        Log.e("tag", "Msg from broadcast:" + msgReceived);
    }
}

Registering Receiver in Manifest

       <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.codeexa.broadcast.ACTION"/>
            </intent-filter>
        </receiver>

Registering Receiver at Run Time

BroadcastReceiver runTimeBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String msgReceived = intent.getStringExtra("message");
            Log.e("tag", "Msg from broadcast:" + msgReceived);
        }
    };

    private void registerBroadcast(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(INTENT_ACTION);
        registerReceiver(runTimeBroadcastReceiver, filter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        registerBroadcast();
    }

    @Override
    protected void onStop() {
        if (runTimeBroadcastReceiver != null) {
            unregisterReceiver(runTimeBroadcastReceiver);
        }
        super.onStop();
    }

Call registerBroadcast function whenever you want to start receiving broadcasts. Don’t forget to call unregisterReceiver when broadcasts are not required.

Local Broadcasts

LocalBroadcastManager can be used to register local broadcasts within the scope of app. It has numerous advantages and is more efficient over global receivers.

Register LocalBroadcastManager Receiver

    private void createLocalBroadcastReceiver(){
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(INTENT_ACTION);
        localBroadcastManager.registerReceiver(runTimeBroadcastReceiver, filter);

    }

Send LocalBroadcastManager Broadcast

   private void sendLocalBroadcast(){
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        Intent broadcastMsg = new Intent(INTENT_ACTION);
        broadcastMsg.putExtra("message", "Sending data through broadcast");
        localBroadcastManager.sendBroadcast(broadcastMsg);

    }

 

Leave a Reply