10
Broadcast Receiver Broadcasts are system wide events Examples – SMS received , battery low, bluetooth, Broadcast-Receiver Handles these events

Broadcast Receiver

Embed Size (px)

DESCRIPTION

Android Broadcast Receiver - presented at beginner level workshop conducted by HasGeek

Citation preview

Page 1: Broadcast Receiver

Broadcast Receiver

● Broadcasts are system wide events● Examples

– SMS received , battery low, bluetooth, ● Broadcast-Receiver

– Handles these events

Page 2: Broadcast Receiver

Broadcast Receiver

● Cannot display User Inferace● Must be handled within 10 seconds● Can start a service for background processing

Page 3: Broadcast Receiver

Broadcast Receiver subclass

class ........ extends BroadcastReceiver{

onReceive( )

{ your code to handle the broadcast event

}

}

Page 4: Broadcast Receiver

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

Page 5: Broadcast Receiver

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

Page 6: Broadcast Receiver

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(); }

}

Page 7: Broadcast Receiver

Extract SMS

● Intent Bundle PDU SMS

Page 8: Broadcast Receiver

Extract SMS

● Intent Bundle PDU SMS

Bundle bundle = intent.getExtras();

Object[] pdusObj = bundle.get("pdus");

SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdusObj[0]);

Page 9: Broadcast Receiver

Extract SMS

● Intent Bundle PDU SMS

Page 10: Broadcast Receiver

Examples of broadcasts● Examples for system events Event Usage● Intent.ACTION_BATTERY_LOW The battery level has fallen below a threshold● Intent.ACTION_BATTERY_OKAY The battery level has risen again● Intent.ACTION_BOOT_COMPLETED Android is up and running● 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 The user changed the language of the device● Intent.ACTION_MY_PACKAGE_REPLACED Your app has been updated● Intent.ACTION_PACKAGE_ADDED A new app has been installed● Intent.ACTION_POWER_CONNECTED The device has been plugged in● 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● AudioManager.ACTION_AUDIO_BECOMING_NOISY The internal audio speaker is about to be used instead

of other output means (like a headset)●