2. BROADCAST & BROADCAST RECEIVERS
• Broadcast 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.
3. • Broadcast Receiver is one of the core component of Android application.
• Broadcast Receivers simply respond to broadcast messages from other
applications or from the system itself.
• These messages are sometime called events or intents.
• Android system sends broadcasts when system events occur such as system
boots up, device starts charging, connectivity changing, low battery etc.
• Some apps can also send custom broadcasts to notify other apps like- data
download completed.
• Unlike Activities, Broadcast Receivers does not have User Interface.
WHAT ARE BROADCAST RECEIVERS?
4. • 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.
5.
6. SYSTEM-GENERATED BROADCASTS
• Some of the important and system-generated intents are-
Intents Description
android.intent.action.BATTERY_CHANGED This keeps track of the battery’s charging state,
percentage, and other information about the
battery.
android.intent.action.BATTERY_LOW It indicates the low battery condition.
android.intent.action.POWER_CONNECTED It indicates that the power is connected to the
device.
android.intent.action.POWER_DISCONNECTE
D
The power is disconnected from the device.
android.intent.action.BOOT_COMPLETED This broadcast is shown only once when the device
boots for the first time.
android.intent.action.CALL This intent is to perform a call to some specific
person, according to data.
7. SYSTEM-GENERATED INTENTS
• Some of the important and system-generated intents are-
Intents Description
android.intent.action.DATE_CHANGED This means the date of the device has changed.
android.intent.action.REBOOT This means that the device has rebooted.
android.intent.action.CONNECTIVITY_CHANG
E
This shows the network connectivity of the device
has changed.
android.intent.action.BUG_REPORT This reports the bugs if there is any.
android.intent.action.CALL_BUTTON The user pressed the call button to make a call,
which takes them to an appropriate user interface.
8. IMPLEMENTING BROADCAST RECEIVERS IN
ANDROID
• To implement a receiver, it need to be register first, which could be done
in the following two ways:
1. Context-registered
• This is also known as the dynamically registering method.
• In this, the registration is done using Context.registerReceiver() method.
• Implementation of Context-registered receiver:
IntentFilter filter = new IntentFilter();
intentFilter.addAction(getPackageName()+"android.net.conn.CO
NNECTIVITY_CHANGE");
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, filter);
9. IMPLEMENTING BROADCAST RECEIVERS IN
ANDROID
2. Manifest-registered
• This is also known as the statistically registering method.
• In this, the registration is done in the manifest file, using <receiver> tag.
• Implementation of Manifest-registered receiver:
<receiver android:name="DataFlairReceiver" >
<intent-filter>
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
10. INTENT & INTENT-FILTERS
IN ANDROID
• The intent is a messaging object which tells what kind of action to be performed. It facilitates
the communication between the components.
• Intents are used to start an activity, start a service and deliver a broadcast.
• Two types of intents are-
• Explicit Intent : In explicit intent user knows about all the things like after clicking a button
which activity will start and Explicit intents are used for communication inside the application
• Implicit Intent : Implicit intents do not name a specific component like explicit intent, instead
declare general action to perform, which allows a component from another app to handle.
• Eg. when you tap the share button in any app you can see the Gmail, Bluetooth, and other
sharing app options.
11. INTENT & INTENT-FILTERS
IN ANDROID
• Intent Filters are expressions in the Manifest file of an Android Application which
decide the behavior of an intent.
• It is used to specify the type of intents that an Activity, service or Broadcast
receiver can respond to.
• It declares the functionality of its parent component (i.e. activity, services or
broadcast receiver).
• Intent filters mentioned in the Manifest file can be nested with the application
components and the type of intents can be specified to accept using these three
elements- action, category and data.
12. INTENT & INTENT-FILTERS
IN ANDROID
• Elements in Intent Filters-
• 1. Action: It represent an activities action, what an activity is going to do. It is declared
with the name attribute as given below-
• <action android:name = "string" />
• An Intent Filter element must contain one or more action element. Action is a string that
specifies the action to perform. You can declare your own action as given below. But we
usually use action constants defined by Intent class.
<intent-filter>
<action android:name="com.example.intentfilters.Main2Activity"/>
</intent-filter>
• For Example – Some of the common actions for starting an activity are ACTION_VIEW,
ACTION_SEND, ACTION_MAIN, ACTION_WEB_SEARCH etc.
13. INTENT & INTENT-FILTERS
IN ANDROID
• Elements in Intent Filters-
• 2. Category: This attribute of Intent filter dictates the behavior or nature of an Intent.
• There is a string which contains some additional information about the intent which will be
handled by a component.
• The syntax of category is as follows:
<category android:name="string" />
• Example of the common categories are-
• BROWSABLE - Browsable category, activity allows itself to be opened with web browser to
open the reference link provided in data.
• LAUNCHER - Launcher category puts an activity on the top of stack, whenever application
will start, the activity containing this category will be opened first.
15. INTENT & INTENT-FILTERS
IN ANDROID
• Elements in Intent Filters-
• 3. Data: There are two forms in which data can be passed, using URI(Uniform Resource
Identifiers) or MIME type of data.
16. The code of Intent Filter is used inside AndroidManifest.xml file for an
activity.