Android Send Custom Broadcast Example

This article include examples about how to create and send custom normal or ordered broadcast. It also illustrate how to abort the broadcast in the ordered broadcast receiver chain. If you do not know about android broadcast, you can read Android Broadcast Overview or Android Broadcast to learn.

1. Android Custom Broadcast Example.

In this example there are three buttons, two custom broadcast receivers and one custom broadcast activity. The two broadcast receiver registered to the same intent-filter action statically in AndroidManifest.xml file.

1.1 Send Custom Normal Broadcast Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/rNbCu1F6H4k

When you click the first button in above picture, it will call below code to send a normal custom broadcast.

Intent intent = new Intent(CUSTOM_BROADCAST_ACTION);
sendBroadcast(intent);

Because the two broadcast receiver is not ordered, so they will receive the broadcast almost at same time.

You can see the toast popup message at screen bottom. The custom receiver one will receive the broadcast event first.

AndroidManifest.xml without setting broadcast receiver priority.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dev2qa.example">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".broadcast.activity.CustomBroadcastActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".broadcast.receiver.CustomBroadcastReceiverOne"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.dev2qa.example.broadcast.activity.CUSTOM_BROADCAST" />
            </intent-filter>
        </receiver>

        <receiver
            android:name=".broadcast.receiver.CustomBroadcastReceiverTwo"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.dev2qa.example.broadcast.activity.CUSTOM_BROADCAST" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

1.2 Send Custom Ordered Broadcast Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/T59FKuHSXgw

When you click the second button, it will use below code to send an ordered broadcast.

Intent intent = new Intent(CUSTOM_BROADCAST_ACTION);
sendOrderedBroadcast(intent, null);

This broadcast will also be received by the configured broadcast receiver order in below AndroidManifest.xml.

The receiver’s intent-filter child tag add a new android:priority attribute. The value the bigger, the first to receive the broadcast in the receiver chain.

You can see receiver two’s priority value is bigger than receiver one, so it will receive the custom broadcast first. The toast popup message at the screen bottom also confirm this.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dev2qa.example">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".broadcast.activity.CustomBroadcastActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".broadcast.receiver.CustomBroadcastReceiverOne"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="9">
                <action android:name="com.dev2qa.example.broadcast.activity.CUSTOM_BROADCAST" />
            </intent-filter>
        </receiver>

        <receiver
            android:name=".broadcast.receiver.CustomBroadcastReceiverTwo"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="10">
                <action android:name="com.dev2qa.example.broadcast.activity.CUSTOM_BROADCAST" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

When you click the third button, it use below code to send an ordered broadcast with extra String type information.

This information will make the first broadcast receiver to abort the broadcast.

Because the receiver two receive the broadcast first, so it will abort the broadcast.

So you can not see toast popup message for custom broadcast receiver one.

Intent intent = new Intent(CUSTOM_BROADCAST_ACTION);
intent.putExtra(ABORT_ORDERED_CUSTOM_BROADCAST, ABORT_ORDERED_CUSTOM_BROADCAST);
sendOrderedBroadcast(intent, null);

2. Custom Broadcast Example Source Code.

2.1 Layout Xml File.

activity_custom_broadcast.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/send_normal_custom_braodcast_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Normal Custom Broadcast"/>

    <Button
        android:id="@+id/send_ordered_custom_braodcast_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Ordered Custom Broadcast"/>

    <Button
        android:id="@+id/abort_ordered_custom_braodcast_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Abort Ordered Custom Broadcast"/>

</LinearLayout>

2.2 Activity Java File.

CustomBroadcastActivity.java

package com.dev2qa.example.broadcast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.dev2qa.example.R;

public class CustomBroadcastActivity extends AppCompatActivity {

    // This is the custom intent-filter action value.
    public static final String CUSTOM_BROADCAST_ACTION = "com.dev2qa.example.broadcast.activity.CUSTOM_BROADCAST";

    public static final String ABORT_ORDERED_CUSTOM_BROADCAST = "ABORT_ORDERED_CUSTOM_BROADCAST";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_broadcast);

        setTitle("dev2qa.com - Send Custom Broadcast Example.");

        // When click it will send a normal custom broadcast.
        Button sendNormalCustomBroadcastButton = (Button)findViewById(R.id.send_normal_custom_braodcast_button);
        sendNormalCustomBroadcastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Send a normal broadcast when being clicked.
                // The broadcast will be received by all the registered broadcast receivers at same time.
                Intent intent = new Intent(CUSTOM_BROADCAST_ACTION);
                sendBroadcast(intent);
            }
        });

        // When click it will send an ordered broadcast.
        Button sendOrderedCustomBroadcastButton = (Button)findViewById(R.id.send_ordered_custom_braodcast_button);
        sendOrderedCustomBroadcastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Send an ordered broadcast when being clicked.
                // The broadcast will be received by the registered broadcast receiver in order.
                // The receiver receive order id defined by the intent-filter priority attribute value.
                // The bigger the value, the first to receive.
                Intent intent = new Intent(CUSTOM_BROADCAST_ACTION);
                sendOrderedBroadcast(intent, null);
            }
        });

        // When click it will send an ordered broadcast also,
        // but the broadcast will be aborted by the first broadcast receiver.
        Button abortOrderedCustomBroadcastButton = (Button)findViewById(R.id.abort_ordered_custom_braodcast_button);
        abortOrderedCustomBroadcastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CUSTOM_BROADCAST_ACTION);
                // This String extra value will indicate the broadcast receiver to abort the broadcast.
                // So the posterior broadcast receiver will not receive it.
                intent.putExtra(ABORT_ORDERED_CUSTOM_BROADCAST, ABORT_ORDERED_CUSTOM_BROADCAST);
                sendOrderedBroadcast(intent, null);
            }
        });
    }
}

2.3 Custom Broadcast Receiver One Java File.

CustomBroadcastReceiverOne.java

package com.dev2qa.example.broadcast.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.dev2qa.example.broadcast.activity.CustomBroadcastActivity;

public class CustomBroadcastReceiverOne extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(CustomBroadcastActivity.CUSTOM_BROADCAST_ACTION.equals(action)) {
            // When receive it will show an toast popup message.
            Toast.makeText(context, "Custom Broadcast Receiver One Receive Message.", Toast.LENGTH_SHORT).show();

            // If the broadcast contains abort command.
            String abortBroadcast = intent.getStringExtra(CustomBroadcastActivity.ABORT_ORDERED_CUSTOM_BROADCAST);
            if (!TextUtils.isEmpty(abortBroadcast)) {
                // Abort the broadcast, posterior broadcast receiver will not receive.
                abortBroadcast();
            }
        }
    }
}

2.4 Custom Broadcast Receiver Two Java File.

CustomBroadcastReceiverTwo.java

package com.dev2qa.example.broadcast.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.dev2qa.example.broadcast.activity.CustomBroadcastActivity;

public class CustomBroadcastReceiverTwo extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(CustomBroadcastActivity.CUSTOM_BROADCAST_ACTION.equals(action)) {
            // When receive it will show an toast popup message.
            Toast.makeText(context, "Custom Broadcast Receiver Two Receive Message.", Toast.LENGTH_SHORT).show();

            // If the broadcast contains abort command.
            String abortBroadcast = intent.getStringExtra(CustomBroadcastActivity.ABORT_ORDERED_CUSTOM_BROADCAST);
            if (!TextUtils.isEmpty(abortBroadcast)) {
                // Abort the broadcast, posterior broadcast receiver will not receive.
                abortBroadcast();
            }
        }
    }
}

1 thought on “Android Send Custom Broadcast Example”

  1. I need help for Broadcast Receiver.I want to do work on click of button and capture the screenshot.After this it would be save in storage.show screenshot in imageview and save in memory would be done during onReceive().please please help. reply me on my mail.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.