In Android, a Broadcast Receiver is an application component that can respond to system-wide broadcast announcements. It’s a powerful feature that allows applications to react to events happening inside the system or other applications. Let’s explore the concept of Broadcast Receivers in Android.
What is a Broadcast Receiver?
A Broadcast Receiver is an Android component that allows an application to register for system events or application events. Once registered, the Broadcast Receiver then becomes a subscriber for these messages (also called Intents).
Implementing a Broadcast Receiver
To implement a Broadcast Receiver, you need to extend the BroadcastReceiver
base class and override the onReceive()
method. The onReceive()
method is called each time the Broadcast Receiver receives a broadcast message.
Here’s a simple example of a Broadcast Receiver that logs a message when it receives a broadcast:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast received");
}
}
Registering a Broadcast Receiver
There are two ways to register a Broadcast Receiver:
Static Registration: This is done via the application’s manifest file. The Broadcast Receiver is registered when the app is installed. This type of registration is useful when you need to respond to broadcasts even when your app is not running.
Dynamic Registration: This is done at runtime, usually from an Activity or Service. The Broadcast Receiver is registered when the code is executed and unregistered when the app is closed. This type of registration is useful when you only need to respond to broadcasts when your app is running.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter> </receiver> </application>
Common Use Cases
Broadcast Receivers are commonly used for:
- Listening for system events, such as boot completed, battery low, airplane mode, etc.
- Listening for application-specific broadcasts, such as download completed, data loaded, etc.
- Communicating between different components of the same application.
Example:
MainActivity.java
package in.infovistar.broadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Context context;
private Button btnBroadCastIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnBroadCastIntent.setOnClickListener(v -> {
startBroadCastIntent();
});
}
private void startBroadCastIntent() {
Intent intent = new Intent();
intent.setAction("in.infovistar.broadcastreceiver.CUSTOM_INTENT"); sendBroadcast(intent);
}
private void init() {
context = this;
btnBroadCastIntent = findViewById(R.id.btn_broadcast_intent);
}
}
MyBroadcastReceiver.java
package in.infovistar.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_broadcast_intent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broadcast Intent" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="in.infovistar.broadcastreceiver.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>
</manifest>