write the use of broadcast and receive
Answers
it
is an android component which let an app to listen for system or application events. Android app can register to receive broadcast. When some broadcast is sent by the system it looks for the apps that have subscribed to receiver those particular type of example.
hope it helps ❤️
Explanation:
Broadcast Receiver
It is an android component which let an app to listen for system or application events.
Android app can register to receive broadcast. When some broadcast is sent by the system it looks for the apps that have subscribed to receiver those particular type of example.
For example when system broadcast ACTION_BOOT_COMPLETED event. The android system looks for the app that have subscribed for this event.
How to implement ?
There are two important steps to make BroadcastReceiver work:
Creating the Broadcast Receiver.
Registering Broadcast Receiver
There are two ways how an application can register receiver.
Statically
Dynamically
Let’s first understand how we can register receiver statically.
Statically
First create the Broadcast Receiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
2. Registering Broadcast Receiver
A broadcast receiver can be registered in manifest file.
A broadcast receiver only listens for those broadcast intent which are actually registered .
For example if want want our broadcast receiver to listen for ACTION_BOOT_COMPLETED event which is fired by Android system then we will have to register our broadcast receiver in Manifest.xml file.
Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.
I think it will help you