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
Components of SMS in Android Manifest file – Permissions – Intent filter Classes within android.telephony package – android.telephony.SmsManager class – android.telephony.SmsMessage class android.content.BroadcastReceiver No Listeners
Manifest file
Permissions uses-permission tags – children of manifest tag – often placed above application tag provides ability to send messages provides ability to receive messages
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
SmsManager class
SmsManager Concrete class Manages SMS activities Needed to send a message – not needed to receive Not instantiated directly – instantiated through the SmsManager getDefault() method SmsManager sm = SmsManager.getDefault();
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 – 1 st Intent done when message sent, 2 nd when message received only first and third arguments are required – others can be null
Sample code Sending a message //sendTo is a String holding the phone number //message is a String holding the text message SmsManager sm = SmsManager.getDefault(); sm.sendTextMessage(sendTo, null, message, null, null); To send a message, only an SmsManager is needed – BroadcastReceiver and SmsMessage are not used
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 , etc.)
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
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[]
Sample code – receiving a message public class MyBroadcastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent i) { Bundle b = i.getExtras(); SmsMessage msg = 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 }
Consequences of not having a Listener
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
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);
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
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