Broadcast Receiver

1,276 views

Published on

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

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,276
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
109
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Broadcast Receiver

  1. 1. Broadcast Receiver ● Broadcasts are system wide events ● Examples – SMS received , battery low, bluetooth, ● Broadcast-Receiver – Handles these events
  2. 2. Broadcast Receiver ● Cannot display User Inferace ● Must be handled within 10 seconds ● Can start a service for background processing
  3. 3. Broadcast Receiver subclass class ........ extends BroadcastReceiver { onReceive( ) { your code to handle the broadcast event } }
  4. 4. 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
  5. 5. 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
  6. 6. 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(); } }
  7. 7. Extract SMS ● Intent Bundle PDU SMS
  8. 8. Extract SMS ● Intent Bundle PDU Bundle bundle = intent.getExtras(); Object[] pdusObj = bundle.get("pdus"); SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdusObj[0]); SMS
  9. 9. Extract SMS ● Intent Bundle PDU SMS
  10. 10. 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)

×