Broadcast Receiver
●

Broadcasts are system wide events

●

Examples
– SMS received , battery low, bluetooth,

●

Broadcast-Receiver
–

Handles these events
Broadcast Receiver
●

Cannot display User Inferace

●

Must be handled within 10 seconds

●

Can start a service for background processing
Broadcast Receiver subclass
class ........ extends BroadcastReceiver
{
onReceive( )
{
your code to handle the broadcast event
}

}
Steps
●

create subclass of BroadcastReceiver

●

onReceive()

●

Get the intent – check if it has SMS info

●

Register our receiver in manifest

●

Set the SMS permissions in manifest
Steps
●

create subclass of BroadcastReceiver

●

onReceive()

●

Register our receiver in manifest

●

Set the SMS permissions in manifest

●

Get the intent – check if it has SMS info
onReceive
String action="android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context c, Intent i) {

final Bundle bundle = i.getExtras();
if(i.getAction().equals(action))
{
Toast.makeText(c, "SMS Received", Toast.LENGTH_SHORT).show();
String msg= parseSMS(bundle);
Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
}
}
Extract SMS
●

Intent

Bundle

PDU

SMS
Extract SMS
●

Intent

Bundle

PDU

Bundle bundle = intent.getExtras();
Object[] pdusObj = bundle.get("pdus");
SmsMessage msg =
SmsMessage.createFromPdu((byte[])
pdusObj[0]);

SMS
Extract SMS
●

Intent

Bundle

PDU

SMS
Examples of broadcasts
●

Examples for system events Event Usage

●

Intent.ACTION_BATTERY_LOW

●

Intent.ACTION_BATTERY_OKAY The battery level has risen again

●

Intent.ACTION_BOOT_COMPLETED

●

Intent.ACTION_DEVICE_STORAGE_LOW Storage space on the device is getting limited

●

Intent.ACTION_DEVICE_STORAGE_OK The storage situation has improved again

●

Intent.ACTION_HEADSET_PLUG A headset was plugged in or a previously plugged headset was removed

●

Intent.ACTION_LOCALE_CHANGED

●

Intent.ACTION_MY_PACKAGE_REPLACED

●

Intent.ACTION_PACKAGE_ADDED A new app has been installed

●

Intent.ACTION_POWER_CONNECTED

●

Intent.ACTION_POWER_DISCONNECTED The device has been disconnected again

●

KeyChain.ACTION_STORAGE_CHANGED The keystore changed

●

BluetoothDevice.ACTION_ACL_CONNECTED A Bluetooth ACL connection has been established

●

●

The battery level has fallen below a threshold
Android is up and running

The user changed the language of the device
Your app has been updated

The device has been plugged in

AudioManager.ACTION_AUDIO_BECOMING_NOISY The internal audio speaker is about to be used instead
of other output means (like a headset)

Broadcast Receiver

  • 1.
    Broadcast Receiver ● Broadcasts aresystem wide events ● Examples – SMS received , battery low, bluetooth, ● Broadcast-Receiver – Handles these events
  • 2.
    Broadcast Receiver ● Cannot displayUser Inferace ● Must be handled within 10 seconds ● Can start a service for background processing
  • 3.
    Broadcast Receiver subclass class........ extends BroadcastReceiver { onReceive( ) { your code to handle the broadcast event } }
  • 4.
    Steps ● create subclass ofBroadcastReceiver ● onReceive() ● Get the intent – check if it has SMS info ● Register our receiver in manifest ● Set the SMS permissions in manifest
  • 5.
    Steps ● create subclass ofBroadcastReceiver ● onReceive() ● Register our receiver in manifest ● Set the SMS permissions in manifest ● Get the intent – check if it has SMS info
  • 6.
    onReceive String action="android.provider.Telephony.SMS_RECEIVED"; public voidonReceive(Context c, Intent i) { final Bundle bundle = i.getExtras(); if(i.getAction().equals(action)) { Toast.makeText(c, "SMS Received", Toast.LENGTH_SHORT).show(); String msg= parseSMS(bundle); Toast.makeText(c, msg, Toast.LENGTH_SHORT).show(); } }
  • 7.
  • 8.
    Extract SMS ● Intent Bundle PDU Bundle bundle= intent.getExtras(); Object[] pdusObj = bundle.get("pdus"); SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdusObj[0]); SMS
  • 9.
  • 10.
    Examples of broadcasts ● Examplesfor system events Event Usage ● Intent.ACTION_BATTERY_LOW ● Intent.ACTION_BATTERY_OKAY The battery level has risen again ● Intent.ACTION_BOOT_COMPLETED ● Intent.ACTION_DEVICE_STORAGE_LOW Storage space on the device is getting limited ● Intent.ACTION_DEVICE_STORAGE_OK The storage situation has improved again ● Intent.ACTION_HEADSET_PLUG A headset was plugged in or a previously plugged headset was removed ● Intent.ACTION_LOCALE_CHANGED ● Intent.ACTION_MY_PACKAGE_REPLACED ● Intent.ACTION_PACKAGE_ADDED A new app has been installed ● Intent.ACTION_POWER_CONNECTED ● Intent.ACTION_POWER_DISCONNECTED The device has been disconnected again ● KeyChain.ACTION_STORAGE_CHANGED The keystore changed ● BluetoothDevice.ACTION_ACL_CONNECTED A Bluetooth ACL connection has been established ● ● The battery level has fallen below a threshold Android is up and running The user changed the language of the device Your app has been updated The device has been plugged in AudioManager.ACTION_AUDIO_BECOMING_NOISY The internal audio speaker is about to be used instead of other output means (like a headset)