00:00

Understanding Broadcast Receivers in Android Development

Broadcast receivers in Android are used to respond to system-wide events like low battery, device booting, incoming calls, or changes in network connectivity. There are two types - static and dynamic receivers. Learn how to create and register a broadcast receiver in your Android application.

blindu
Download Presentation

Understanding Broadcast Receivers in Android Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Broadcast Receiver

  2. Broadcast Receiver • Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events. Broadcast Receivers allow us to register for the system and application events, and when that event happens, then the register receivers get notified. There are mainly two types of Broadcast Receivers: • Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. • Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.

  3. Cont.. Intent Description Of Event android.intent.action.BATTERY_LOW : Indicates low battery condition on the device. This is broadcast once after the system has finished booting android.intent.action.BOOT_COMPLETED To perform a call to someone specified by the data android.intent.action.CALL android.intent.action.DATE_CHANGED Indicates that the date has changed android.intent.action.REBOOT Indicates that the device has been a reboot The mobile network or wifi connection is changed(or reset) android.net.conn.CONNECTIVITY_CHANGE android.intent.ACTION_AIRPLANE_MODE_CHAN GED This indicates that airplane mode has been switched on or off.

  4. Cont.. • The two main things that we have to do in order to use the broadcast receiver in our application are: • Creating the Broadcast Receiver: class AirplaneModeChangeReceiver:BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { // logic of the code needs to be written here } } • Registering a BroadcastReceiver IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED).also { // receiver is the broadcast receiver that we have registered // and it is the intent filter that we have created registerReceiver(receiver,it) }

  5. Example • Step 1: Create a New Project • To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. • Step 2: Working with the activity_main.xml file • Go to the activity_main.xml file .

  6. Step 4: Create a new class • Go to app > java > your package name(in which the MainActicity is present) > right-click > New > Java/Class and name the files as AirplaneModeChangeReceiver. Below is the code for the AirplaneModeChangeReceiver file

  7. Step 4: Working with the MainActivity file

More Related