Android Application Component: BroadcastReceiver Tutorial

27,564 views

Published on

Published in: Education, Technology, Business
0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
27,564
On SlideShare
0
From Embeds
0
Number of Embeds
2,503
Actions
Shares
0
Downloads
811
Comments
0
Likes
17
Embeds 0
No embeds

No notes for slide

Android Application Component: BroadcastReceiver Tutorial

  1. 1. Android Application Development<br />Broadcast Receiver<br />Ahsanul Karim<br />ahsanul.karim@sentinelbd.com<br />Sentinel Solutions Ltd.<br />http://www.sentinelbd.com<br />
  2. 2. Application Building Blocks<br />
  3. 3. Android Application Anatomy<br />Activities<br />Provides User Interface<br />Usually represents a Single Screen<br />Can contain one/more Views<br />Extendsthe Activity Base class<br />Services<br />NoUser Interface<br />Runs in Background<br />Extends the Service Base Class<br />Intent/Broadcast Receiver<br />Receives and Reacts to broadcast Intents<br />No UI but can start an Activity<br />Extends the BroadcastReceiver Base Class<br />Content Provider<br />Makes application data available to other apps<br />Data stored in SQLite database<br />Extends the ContentProviderBase class<br />Application= Set of Android Components<br />
  4. 4. Broadcast Receivers<br />A broadcast receiver is a component that responds to system-wide <br />Broadcast announcements. <br />Many broadcasts originate from the system—for example, a <br />Broadcast announcing that the screen has turned off, the battery <br />is low, or a picture was captured or an SMS is received. <br />Applications can also initiate broadcasts—for example, to let other <br />Applications know that some data has been downloaded to the <br />device and is available for them to use. <br />Although broadcast receivers don't display a user interface, they may <br />create a status bar notification to alert the user when a <br />broadcast event occurs. <br />More commonly, though, a broadcast receiver is just a "gateway" to <br />other components and is intended to do a very minimal amount of <br />work. For instance, it might initiate a service/or start an activity to <br />perform some work based on the event.<br />
  5. 5. Android Application Anatomy<br />Activity 1 <br />Activity 2 <br />BroadcastReceiver<br />Service<br />UI<br />OS<br />Intents<br />Directed Intents<br />Broadcast Intents<br />BIG PICTURE<br />
  6. 6. Broadcast Receivers<br />Activity<br />We’ll use a Broadcast Receiver to capture SMS receive event<br />We capture the SMS receive event and launch an Activity to show the sms and give user <br />an option to reply the SMS<br />BroadcastReceiver<br />OS<br />
  7. 7. Broadcast Receivers<br />Create a new project BroadcastReceiverDemo<br />A broadcast receiver is implemented as a subclass of BroadcastReceiver and each <br />broadcast is delivered as an Intent object. In this case the intent is detected by <br />android.provider.Telephony.SMS_RECEIVED<br />To do this we’ll create a class SMSReceiver that extends BroadcastReceiver class and define the method onReceive()<br />BroadcastReceiver<br />
  8. 8. Broadcast Receivers (Contd.)<br />3. We also need to add SMSReceiver as receiver of a particular Intent (SMS received) which is identified by android.provider.Telephony.SMS_RECEIVED<br />BroadcastReceiver<br />
  9. 9. Broadcast Receivers (Contd.)<br />4. Also we have to add permission for receiving SMS<br />BroadcastReceiver<br />
  10. 10. Broadcast Receivers (Contd.)<br />5. Now we run the application<br />6. Now we use emulator control to send sms<br />
  11. 11. Receiving SMS<br />Bundle bundle = intent.getExtras(); <br />SmsMessage[] msgs = null;<br />String str = "";<br />String address="";<br />if (bundle != null)<br />{<br />//---retrieve the SMS message received---<br />Object[] pdus = (Object[]) bundle.get("pdus");<br />msgs = new SmsMessage[pdus.length];<br />for (int i=0; i<msgs.length; i++)<br /> {<br />msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); <br />str += "SMS from " + msgs[i].getOriginatingAddress(); <br />str += " :";<br />str += msgs[i].getMessageBody().toString();<br />str += "n"; <br /> address=msgs[i].getOriginatingAddress();<br />Toast.makeText(context, str, Toast.LENGTH_LONG).show();<br /> }<br />
  12. 12. Sending SMS<br />1. Add permission in menifest.xml<br />2. We add the following code for sending SMS from anywhere of our application<br />
  13. 13. Exercise<br />We’ll create a replica of SMS application of Android<br />Application will have a basic TabActivity with 3 tabs (Activities)<br />Send- will give user option to send sms (2 input fields for number and text)<br />All sent SMS will be saved in database<br />Inbox- (List Activity) which will fetch all received SMS from database<br />Sent- (ListActivity) which will fetch all sent SMS<br />A broadcast receiver which will receive SMS and save them to database<br />

×