Your SlideShare is downloading. ×
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Android Application Component: BroadcastReceiver Tutorial

23,003

Published on

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

No Downloads
Views
Total Views
23,003
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
627
Comments
0
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×