Sunday, 30 December 2012

How to send SMS using Android SDK

At its simplest, it just takes about 2 lines of code to send an SMS using the SDK.

Firstly, the manifest file should have the following permission
<uses-permission android:name="android.permission.SEND_SMS"/>
Next, you create a method to send the SMS as follows,
/**
 * sendSMS accepts two obvious parameters
 * A phone number, and a message
 */
private void sendSMS(String phoneNumber, String message){
    SmsManager sms=SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}
That is all. Now you can call this method from your Activity(s) and the message will be sent to the give phone number. In case you are using emulators, you can pass the emulator numbers like 5554 or 5556. You will need two emulators running to be able to test this.

1 comment:

  1. Thank you for great post. I have been looking for this tutorials for months.

    ReplyDelete