2. Broadcast Receiver.. 🤔
1. A broadcast receiver is a internal trigger which activated by Android
runtime whenever any event occurred from Android system.
2. Many broadcasts originate from the system—for example, a Broadcast
announcing that the screen has turned off, the battery is low, or a WIFI
is ON/OFF or an SMS is received.
3. Applications can also initiate broadcasts—for example, to let other
Applications know that some data has been downloaded to the device and
is available for them to use.
5. Broadcast
Receiver
A broadcast receiver (receiver)
is an Android component which
allows you to register for system
or application events. All
registered receivers for an
event are notified by the
Android runtime once this event
happens.
6. Broadcast
Receiver
We’ll use a Broadcast Receiver
to capture WIFI event.
We capture the WIFI ON/OFF
events and launch an Activity to
show the WIFI state and give
user an option to ON/OFF the
WIFI.
7. Broadcast
Receiver
A broadcast receiver is
implemented as a subclass of
BroadcastReceiver.
BroadcastReciver is a abstruct
class and we need to override
a method.
onReceive(Context context,
Intent intent)
This method is called when the
BroadcastReceiver is receiving
an Intent broadcast.
8. Broadcast
Receiver
we have to add two permissions
in Manifest file for receiving
WIFI ON/OFF Events.
i. CHANGE_WIFI_STATE
ii. ACCESS_WIFI_STATE
9. Key classes and methods
1) WifiManager - This class provides the primary API for managing all aspects of Wi-Fi
connectivity.
2) WifiManager.WIFI_STATE_CHANGED_ACTION - Broadcast intent action indicating
that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra
provides this state as an int. Another extra provides the previous state, if available.
3) registerReceiver- Register a BroadcastReceiver to be run in the main activity thread.
The receiver will be called with any broadcast Intent that matches filter, in the main
application thread.
4) unregisterReceiver - Unregister a previously registered BroadcastReceiver. All filters
that have been registered for this BroadcastReceiver will be removed.
5) BroadcastReceiver: Base class for code that receives and handles broadcast intents.