1 / 22

SMS

SMS. SMS. Short Message Service Primarily text messages between mobile phones First one sent December 3, 1982 “Merry Christmas” In 2008 Approximately 2.5 billion active users Over 4.1 trillion messages sent Average cost to users: 0.11USD Average cost to providers: less than 0.01USD.

menora
Download Presentation

SMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SMS

  2. SMS • Short Message Service • Primarily text messages between mobile phones • First one sent December 3, 1982 • “Merry Christmas” • In 2008 • Approximately 2.5 billion active users • Over 4.1 trillion messages sent • Average cost to users: 0.11USD • Average cost to providers: less than 0.01USD

  3. Components of SMS in Android • Manifest file • Permissions • Intent filter • Classes within android.telephony package • android.telephony.SmsManager class • android.telephony.SmsMessageclass • android.content.BroadcastReceiver • No Listeners

  4. Manifest file

  5. Permissions • uses-permission tags • children of manifest tag • often placed above application tag <uses-permission android:name="android.permission.SEND_SMS"/> provides ability to send messages <uses-permission android:name="android.permission.RECEIVE_SMS"/> provides ability to receive messages

  6. Intent filter • receiver, intent-filter, and action tags • receiver is child of application tag • intent-filter is child of receiver tag • action is child of intent-filter tag • intent-filter ‘filters’ all possible components that can be received and delivers only those specified • name attribute must be set to the name of class that will be receiving messages • class must be a subclass of BroadcastReceiver <receiver android:name=“.MyBroadcastReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>

  7. SmsManager class

  8. SmsManager • Concrete class • Manages SMS activities • Needed to send a message • not needed to receive • Not instantiated directly • instantiated through the SmsManagergetDefault() method SmsManagersm = SmsManager.getDefault();

  9. SmsManager • important methods • getDefault • returns an instance of SmsManager • sendTextMessage (5 parameters) • String indicating destination (i.e. a phone number) • String indicating the source (i.e. a phone number) • String holding the message • 2 PendingIntents • Used when multiple apps are communicating with each other • PendingIntents are Intents that can be handed to other apps with your apps permissions • Here used to pass additional information, or to verify successful communication • 1st Intent done when message sent, 2nd when message received • only first and third arguments are required – others can be null

  10. Sample code • Sending a message //sendTo is a String holding the phone number //message is a String holding the text messageSmsManagersm = SmsManager.getDefault(); sm.sendTextMessage(sendTo, null, message, null, null); • To send a message, only an SmsManager is needed • BroadcastReceiver and SmsMessage are not used

  11. SmsMessage class

  12. SmsMessage class • Concrete class • Used in conjunction with a BroadcastReceiver to receive messages • Represents a specific message • Contains: • sender’s phone • message • other information (timestamp, whether or not the message was sent from e-mail, etc.)

  13. SmsMessage class • important methods • createFromPDU() • creates an SmsMessage from a PDU obtained from the BroadcastReceiver • PDU – protocol data unit (any transferrable entity) • in our case a message • in other network apps may be an address, packet, etc. • getOriginatingAddress() • returns phone number of sender as a String • getMessageBody() • returns message as a String

  14. BroadcastReceiver class

  15. BroadcastReceiver class • Concrete class • Receives incoming messages • messages are stored in an Intent created by the transmitter • to obtain SmsMessage – override onReceive method in BroadcastReceiver class: • getExtras() method in the Intent class returns a Bundle • get(“pdus”) method in the Bundle class returns an Object • must be cast to an Object[] • SmsMessage now created via createFromPdu method • use first element in object array, cast to a byte[]

  16. Sample code – receiving a message public class MyBroadcastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent i) { Bundle b = i.getExtras(); SmsMessagemsg = null; String phone = new String(); String message = new String(); if (b != null) { if (b.get("pdus") instanceof Object[]) { Object[] pdus = (Object[])b.get("pdus"); //Large message may be broken up in parts //iterate thru pdus array if necessary msg = SmsMessage.createFromPdu((byte[])pdus[0]); phone = msg.getOriginatingAddress(); message = msg.getMessageBody(); //Code handling the phone and message goes here } } } }

  17. Consequences of not having a Listener

  18. Listening for events • Historically 2 approaches • Providing a Listener interface • Preferred approach • OnClickListener • LocationListener • SensorEventListener • etc. • Providing the event producer with a handle to the listening object

  19. Sample code – providing a handle to the Listener • In the event producing class private static MyListener observer; public static final void setObserver(MyListener observer) { MyBroadcastReceiver.observer = observer; } • In the listener (in onCreate()) MyBroadcastReceiver.setObserver(this);

  20. Application Structure

  21. Application Structure • In the ‘Listening’ class • In our case this is the main Activity • ‘this’ is past into the ‘setObserver’ method of the BroadcastReceiver subclass • if messages can be sent, there is functionality to send a message using an instance of SmsManager • e.g. this method might be tied to a button via onClick • if messages can be received, there is functionality that handles the actual message • placed in TextView, stored in db, etc. • this method typically called from receiver class • can also be called from listening class if needed

  22. Application Structure • In the BroadcastReceiver subclass • static class variable with the type of the ‘Listening’ class • in our case, an instance of the main Activity • variable typically called ‘observer’ • static method to initialize this instance • argument is an instance of the ‘Listening’ class • method typically called ‘setObserver’ • onReceive method is overridden • signature: public void onReceive (Context c, Intent i) • SmsMessage is instantiated via createFromPdu class method in SmsMessage class • Pertinent information is obtained and sent back to the observer to handle accordingly

More Related