Top Banner
SMS
22

SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

SMS

Page 2: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 3: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 4: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

Manifest file

Page 5: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 6: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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>

Page 7: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

SmsManager class

Page 8: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

SmsManager

• Concrete class• Manages SMS activities• Needed to send a message– not needed to receive

• Not instantiated directly– instantiated through the SmsManager getDefault()

methodSmsManager sm = SmsManager.getDefault();

Page 9: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 10: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 11: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

SmsMessage class

Page 12: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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.)

Page 13: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 14: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

BroadcastReceiver class

Page 15: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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[]

Page 16: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

Sample code – receiving a messagepublic 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 }

} }

}

Page 17: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

Consequences of not having a Listener

Page 18: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 19: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

Sample code – providing a handle to the Listener

• In the event producing classprivate static MyListener observer;

public static final void setObserver(MyListener observer) {MyBroadcastReceiver.observer = observer;

}

• In the listener (in onCreate())MyBroadcastReceiver.setObserver(this);

Page 20: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

Application Structure

Page 21: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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

Page 22: SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.

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