How to send and receive a SMS in Android ?
With all the features supported by smartphones, sometimes we forget the presence of the word phone in smartphone. Thus, it could be important sometimes to use SMS feature of Android devices.
Android SDK provides a very good support for SMS and globally for telephony. In that tutorial, you will learn to send and receive SMS in Android.
1. Send SMS in Android
To manage telephony, all the API is under the package android.telephony . For SMS, entry point is the class SmsManager at the root of the package API.
Thanks to static method getDefault(), you’ll get the default instance for SmsManager. Then, you must check if your message is longer than 160 caracters because SMS simple message is limited to 160 caracters. Beyond this limit, you must send multipart text message. By luck, SmsManager can divide for you a long message to a list of shorts messages.
Code to send SMS is like this :
private static String SENT = "SMS_SENT"; private static String DELIVERED = "SMS_DELIVERED"; private static int MAX_SMS_MESSAGE_LENGTH = 160; public void sendSMS(String phoneNumber, String message) { PendingIntent piSent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0); PendingIntent piDelivered = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(DELIVERED), 0); SmsManager smsManager = SmsManager.getDefault(); int length = message.length(); if (length > MAX_SMS_MESSAGE_LENGTH) { ArrayList<String> messagelist = smsManager.divideMessage(message); smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null); } else { smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered); } }
Be sure to add the SEND_SMS permission to your Android Manifest like this :
<uses-permission android:name="android.permission.SEND_SMS" />
2. Receive a SMS in Android
Second step is to receive a SMS in Android. To achieve that, you must use a BroadcastReceiver implementation filtering the intent android.provider.Telephony.SMS_RECEIVED . On the onReceive method of the BroadcastReceiver, you’ll check the action received. Then, you can data bundled in intent.
Data received are bundled in an array of Object that you will get thanks to getSerializableExtra method of Intent instance.
In Android, a SMS message is modeled by the SmsMessage class. With the data previously obtained, you’re going to create SmsMessage instances thanks to static method createFromPdu of the SmsMessage class. Note that PDU (Protocol Description Unit) is the default format for SMS.
SmsMessage class offers to developers methods to get sender and message received. Code you must implement is like this :
public class SMSReceiver extends BroadcastReceiver { private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; private Intent mIntent; public void onReceive(Context context, Intent intent) { mIntent = intent; String action = intent.getAction(); if (action.equals(ACTION_SMS_RECEIVED)) { String address = null, str = ""; SmsMessage[] msgs = getMessagesFromIntent(mIntent); if (msgs != null) { for (int i = 0; i < msgs.length; i++) { address = msgs[i].getOriginatingAddress(); str += msgs[i].getMessageBody().toString(); str += "\n"; } } if (address != null) { // manage message and address ... } } } public static SmsMessage[] getMessagesFromIntent(Intent intent) { Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i]); } return msgs; } }
To declare your SMSReceiver in the Android Manifest, you can use the following lines in the application tag :
<receiver android:name="com.ssaurel.sendmail.SMSReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>
Here also, you must define the permission to receive SMS in your Android Manifest :
<uses-permission android:name="android.permission.RECEIVE_SMS" />
3. Final Result
You can now test the code in your application and enjoy the power of the SMS management in Android.
Leave a Reply
You must be logged in to post a comment.