- What is Broadcast receiver in android:-Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself.There are following two important steps to make Broadcast Receiver works for the system broadcast intents:
- Creating the Broadcast Receiver.
- Registering Broadcast Receiver.
Creating the Broadcast Receiver:-
A broadcast receiver is implemented as a subclass of Broadcast Receiver class and overriding the onReceive() method where each message is received as a Intent object parameter.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
Registering Broadcast Receiver
An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
Types of Broadcast receiver in android:-
- android.intent.action.BATTERY_CHANGED
- android.intent.action.BATTERY_LOW
- android.intent.action.BATTERY_OKAY
- android.intent.action.BOOT_COMPLETED
- android.intent.action.BUG_REPORT
- android.intent.action.CALL
- android.intent.action.CALL_BUTTON
- android.intent.action.DATE_CHANGED
- android.intent.action.REBOOT