Telephony and SMS
Prepared By : Mr.Milan V. Doshi
UNIT 5 ch-15
T
O
P
I
C
S
TELEPHONY
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.
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
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);
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”/>
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.
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);
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.
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);
Continue…
To use this ACTION_CALL intent, we need a permission in our application manifest
android.permission.CALL_PHONE
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.
Continue…
To replace native dialers involves two step process
Continue…
intent.ACTION_CALL_BUTTON
intent.ACTION_DIAL
intent.ACTION_ACTION_VIEW
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.
Reading Phone Device Details
We can obtain the phone type , software version and the phone’s phone number
Except the 1st all other need following permission
android.permission.READ_PHONE_STATE
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.
Reading SIM Details
When our application is running on GSM device, we can query for the SIM details.
android.permission.READ_PHONE_STATE , required for last method.
Reading Data Connection
We can find the current data connection and data transfer activity
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
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.
Continue…
Monitoring incoming calls
We can override the onCallStateChanged() method in the PhoneStateListener and register it to receive notification when the call state changes.
Continue…
The onCallStateChanged() receives the phone number associated with incoming call and the state parameter representing call state that can be
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
SMS and MMS
NJSMTI
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.
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.
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.
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
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
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);
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.
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.
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.
Continue…
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.
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.
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
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().
Continue…
Each SMSMessage contains details such as sender’s number, message body, timestamp, which can be retrieved by methods getMessageBody() , getTimestampMillis() and getOriginatingAddress().
Continue…
Continue…
To listen for incoming messages , we need to register our broadcast receiver using an intent filter in manifest file.
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.