Test Script

Saturday, December 7, 2013

How To Send SMS In Android

Hi guys, today in this tutorial we will be looking at how you can send SMS in Android through your next killer app you are designing.



You can use two methods to send out SMS :

1. SmsManager API

2. Built In SMS Application

Using the SmsManager API - using this API you can send SMS directly from within your apps. Here is how

SmsManager smsManager = SmsManager.getDefault();
 smsManager.sendTextMessage("1234567890", null, "SMS Text goes here!", null, null);


That's it, its that simple !
1.  The first parameter is where you send the mobile number to which you wish to send the SMS to.
2.  The 3rd parameter contains the text of the SMS to be sent out.
3.  We create an instance of the SmsManager and using the sendTextMessage method we send out the SMS.
This will send the message using the user's credits, so it is always advised to confirm with the user once before sending out the SMS.

Using the Built-In SMS App - using this method you can redirect the user from your application to the default Messaging app in the device where the user can proceed further as needed.

Here is how we can invoke the default SMS app :

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
 sendIntent.putExtra("sms_body", "The SMS text goes here!"); 
 sendIntent.setType("vnd.android-dir/mms-sms");
 startActivity(sendIntent);

Here is what we did :

1.  Create a new intent with the action as Intent.ACTION_VIEW
2.  Set the body of the SMS using the putExtra method, make sure you put the key as " sms_body".
3.  Set the type to  "vnd.android-dir/mms-sms".
4.  Start the activity.

The user will now be redirected to his default/chosen SMS app. Also for both the above methods to work you need to have the Send SMS permission so add the following permission in the Android Manifest :



That's all there is to send out a SMS through your app. Also if your targeting devices on Android 4.4 and above make sure to check this link :  Android 4.4 SMS Changes

Feel free to drop me any comments/queries. 
UA-42774700-1 Twitter Bird Gadget