1 of 45

Telephony and SMS

Prepared By : Mr.Milan V. Doshi

UNIT 5 ch-15

2 of 45

  • Introduction
  • Hardware Support for telephony
  • Using telephony
  • Introducing SMS and MMS

T

O

P

I

C

S

3 of 45

TELEPHONY

4 of 45

Introduction

Android provides telephony API to monitor mobile voice and data connections as well as incoming and outgoing calls, and to send and receive short messaging service messages.

Additionally Android also offers full access to SMS functionality, letting us to send and receive SMS messages from within our applications.

5 of 45

Not all android devices supports telephony(may be a Wi-Fi only android device). We can check for availability of telephony hardware on the device. We can use the hasSystemFeature() method to check the support and also query the existence of CDMA and GSM.

Hardware Support for Telephony

6 of 45

Checking for Telephony hardware

PackageManager pm = getPackageManager();

boolean tSupported = pm.hasSystemFeature(PackageManager. FEATURE_TELEPHONY);

boolean gsmSupported = pm.hasSystemFeature(PackageManager. FEATURE_TELEPHONY_CDMA);

boolean cdmaSupported = pm.hasSystemFeature(PackageMan- ager.FEATURE_TELEPHONY_GSM);

7 of 45

NJSMTI

Marking telephony as a required hardware feature

It might be the case that our application needs telephony support to work. So we must specify that our application requires telephony support to function, we can add a uses-feature node to our application manifest.

<uses-feature android:name=“android.hardware.telephony” android:required=“true”/>

8 of 45

Using Telephony

The Android telephony APIs let our application access the underlying telephone hardware stack, making it possible to create our own dialer, integrate call handling and phone state monitoring into our application.

9 of 45

Initiate Phone Calls

We can initiate a call from the device on which our application is executing. The best way to do this is to use an Intent.ACTION_DIAL intent specifying the number to dial by setting the intents data using a tel: schemas

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(“tel:9879475930”));

startActivity(intent);

10 of 45

Continue…

The code on previous slide will launch the dialer with the number specified. This default dialer lets the user to change the number before explicitly initialing the call. So ACTION_DIAL intent doesn’t require any special permission.

11 of 45

Another way

There is one more way to initiate the call in which the call will be initiate without launching the dialer. In this case the user wont be able to change the number. It can be done as

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(“tel:9879475930”));

startActivity(intent);

12 of 45

Continue…

To use this ACTION_CALL intent, we need a permission in our application manifest

android.permission.CALL_PHONE

13 of 45

Replacing the native dialer

When we use ACTION_DIAL intent it launches the device dialer and provide the call button to initiate the call. We can replace this native dialer with our own designed dialer and in which when client select to dial we can use the device’s native calling feature or code our own calling feature.

14 of 45

Continue…

To replace native dialers involves two step process

  • Intercept intents by the native dialer
  • Initiate and mange outgoing calls
  • We can define our activity which will work as a dialer. To execute this activity on client’s request for dialing, we should use intent-filer in our application’s manifest.

15 of 45

Continue…

  • The following intent filters should be specified for our activity to be initiated

intent.ACTION_CALL_BUTTON

intent.ACTION_DIAL

intent.ACTION_ACTION_VIEW

  • Once our activity has started, it should provide a UI for client to enter the number and a way to place a call.

16 of 45

Accessing Telephony Properties

We can access the telephony properties and phone state using methods of TelephonyManager. We can access the telephony manager on the device running our application as

String service_name = Context.TELEPHONY_SERVICE;

TelephonyManager TM = (TelephonyManager) getSystemService(service_name);

Using it we can access phone properties including device, network, SIM and date state details.

17 of 45

Reading Phone Device Details

We can obtain the phone type , software version and the phone’s phone number

  • getPhoneType() , CDMA, GSM, etc.
  • getDeviceId() , IMEI for GSM or MEID for CDMA.
  • getDeviceSoftwareVersion() , software version
  • getLine1Number() , phone number

Except the 1st all other need following permission

android.permission.READ_PHONE_STATE

18 of 45

Reading Network Details

When the device is connected to a network, we can read mobile country code, mobile network code, country ISO code, network operator name and the type of network connected.

  • getNetworkCountryIso() , county ISO code
  • getNetworkOperator() , MCC +MNC
  • getNetworkOperatorName() , n\w operator name
  • getNetworkType() , network type

19 of 45

Reading SIM Details

When our application is running on GSM device, we can query for the SIM details.

  • getSimState() , absent –ready , always a good idea to obtain prior
  • getSIMCountryIso() , ISO code of SIM country
  • getSIMOperator() , operator code MCC + MNC
  • getSIMOperatorName() , name of the SIM operator
  • getSIMSerialNumber() , serial number of SIM

android.permission.READ_PHONE_STATE , required for last method.

20 of 45

Reading Data Connection

We can find the current data connection and data transfer activity

  • getDataActivity() , in-out-inout-none
  • getDataState() , connected-connecting-disconnected-suspended

21 of 45

Monitoring state changes in phone state

We can monitor the state changes such as listening to an incoming call. This and other state changes are monitored using the PhoneStateListener where some state changes are also broadcasted using intents.

To monitor such state changes our application must have the following permission

android.permission.READ_PHONE_STATE

22 of 45

Mechanism

We need to define a class that implements the PhoneStateListener and override the event handlers which we want to listen for.

We can register this class to listen events with telephony manager using listen() method in which we need to pass the events which we want to listen. A sample code to register is shown on next slide.

23 of 45

Continue…

24 of 45

Monitoring incoming calls

We can override the onCallStateChanged() method in the PhoneStateListener and register it to receive notification when the call state changes.

25 of 45

Continue…

The onCallStateChanged() receives the phone number associated with incoming call and the state parameter representing call state that can be

  • CALL_STATE_IDLE , neither ringing nor in a call
  • CALL_STATE_RINGING , phone is ringing
  • CALL_STATE_OFFHOOK , currently in a call

26 of 45

Tracking cell location changes

We can get notifications whenever cell location changes by overriding onCellLocationChanged(). To listen to this event we need to take permission

android.permission.ACCESS_COARSE_LOCATION

27 of 45

SMS and MMS

NJSMTI

28 of 45

Introduction

SMS technology is used to send short text messages between mobile phones. It provides support for sending both text messages and data messages.

MMS allows users to send and receive messages that include multimedia attachments such as photos, videos and audio.

29 of 45

Using SMS

SMS message delivery is not timely. Using SMS to pass data messages between applications is slow, possibly expensive and can suffer from high latency.

SMS is not suitable for anything that requires real time responsiveness.

30 of 45

Sending SMS and MMS using Intent

We can use an intent to send SMS and MMS using the native application. We need to create an intent with ACTION_SENDTO action intent.

  • Create an intent with ACTION_SENDTO
  • Specify a target number using sms: schema
  • Specify the message to send within the intent payload using an SMS_BODY extra

31 of 45

Continue…

Intent smsIntent = newIntent(Intent.ACTION_SENDTO , Uri.parse(“sms:9879475930”));

smsIntent.putExtra(“sms_body”,”Press send to send me”);

startActivity(smsIntent);

To send MMS

32 of 45

Sending SMS using SMSManager

If we don’t want to use the native SMS application,we can use the SMSManager class to send the SMS. We can retrieve an instance of SMSManager using static getDefault() method

SmsManager smsManager = SmsManager.getDefault();

To use SMS API we need to take permission

android.permission.SEND_SMS

33 of 45

Sending Text Messages

To send a text message, we can use sendTextMessage() of the SMSManager and pass the number of recipient and the text message we want to send.

SmsManager smsManager = SmsManager.getDefault();

String sendTo = “9879475930”;

String myMessage = “THE SUN RISES IN THE EAST”;

smsManager.sendTextMessage(sendTo,null,myMessage,

nul,null);

34 of 45

Continue…

The 2nd argument in sendTextMessage() is the service center to use. If we want to use default of the device we can pass it null. The last two arguments are intents to track the transmission and successful delivery of our messages.

35 of 45

Tracking and Confirming SMS message delivery

To track the transmission and delivery of our outgoing SMS messages , we need to implement and register Broadcast Receivers. These receivers listen for the actions we specify in pending intents (two) passed as argument to sendTextMessage().

The 1st pending intent is fired when the message is either successfully sent or fails.

36 of 45

Continue…

The 2nd pending intent is fired only after recipient receives message. On success of 1st pending intent result code Activity.RESULT_OK is returned.

37 of 45

Continue…

38 of 45

Confirming to the Maximum SMS size

The maximum length of each SMS text message can vary by carrier(mostly 165 chars). So longer messages can be broken down into small parts using the divideMessage() method of SMSManager which returns an Array list of messages. Next we can use the method sendMultipartTextMessage() of SMSManager to send message.

39 of 45

Send Data message

We can send binary data via SMS using the sendDataMessage() which works similar to sendTextMessage() but includes additional parameter for byte array and port number.

40 of 45

Listening for incoming messages

When a device receives a new SMS message , a new Broadcast intent is fired with the android.provider.Telephony.SMS_RECEIVED action.

For an application to listen for SMS broadcast intents , it need to specify the permission

android.permission.RECEIVE_SMS

41 of 45

Continue…

The incoming message is packed as array of SMSMessage. To extract this message(s) from the broadcast intent bundle, use the “pdu” key which gives the SMS PDUs array each of which represents an SMS message from the extras bundle. This PDU byte array needs to be converted into an SMS object using createFromPdu().

42 of 45

Continue…

Each SMSMessage contains details such as sender’s number, message body, timestamp, which can be retrieved by methods getMessageBody() , getTimestampMillis() and getOriginatingAddress().

43 of 45

Continue…

44 of 45

Continue…

To listen for incoming messages , we need to register our broadcast receiver using an intent filter in manifest file.

45 of 45

Handling Data SMS Messages

We can handle and extract the data messages in the same way as normal SMS text messages. To extract the data transmitted within a data SMS, we can use the getUserData() method as follow

byte[] data = msg.getUserData();

This method returns a byte array of the data included in the message.