Skip to content

Sending SMS from Application

January 8, 2010

Hey everyone,

So in this post, I thought I would show you guys two basic examples for how to deal with sending SMS messages from within your application. Also, I’ll point out some of the difficulties that I learned, which weren’t documented.

Method #1

SmsManager sm = SmsManager.getDefault();
// here is where the destination of the text should go
String number = "6508570720";
sm.sendTextMessage(number, null, "Test SMS Message", null, null);

Now, note that this is the most BASIC way to deal with this. You’ll notice that several of the input parameters are null and that’s because you can send optional Pending Intents with the text message to keep track of things such as whether or not the message sent was successful, etc. (For more on this, a good site to conduct would be Advanced SMS Example.)

To test this, one can open two instances of your emulator (one will be port 5554 and one will start on port 5556) and letting the destination number be the port number (i.e. String number = “5556”) will simulate the sending of the text message.

Now, one little thing to note which I figured out but which I don’t think is documented, is that your message MUST be less than or equal to 160 characters. If it’s greater than 160 characters, the method sendTextMessage will throw a Null Pointer Exception.

This method will allow you to immediately send a text with your predetermined message, but let’s say you want to just open the default SMS screen and you want to allow the user to type their own text message, then you can:

Method #2

public class SendSMSActivity extends Activity {

     @Override
      public void onCreate(Bundle savedInstance) {
          // other stuff

          // the destination number
          String number = "6508570720";
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
      }

}

And this will bring up the default SMS sending screen.

Now, one last thing to mention – the permissions. For Method #1, in order for it to work you’re going to need to add the permission

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

However, for Method #2, you don’t need to add any additional permissions as it is simply an ACTION_VIEW intent.

So, between this post and the Advanced SMS Tutorial linked above, I hope this solves all of your SMS sending problems!

Happy coding.

– jwei

24 Comments leave one →
  1. James permalink
    January 18, 2010 2:20 pm

    Hi JWIE, I put method 1 in my Java project but it threw up a few errors. I created a brand new project and and inserted the code but it still created errors; am I missing something. Do I need to wrap tags around it? As you can tell I am new to this. Thanks for posts like this which help make it easier for people like me.

    Cheers,

    James

    • January 25, 2010 12:06 am

      Hey James,

      Sorry for the late response – I hope it’s not too late.

      What exactly is the error you’re getting? Let me know.

      – jwei

  2. August 31, 2010 8:16 pm

    Well jwei512 has explained it nicely, but its a basic tutorial .

    You can go to the below website for step by step advanced tutorial.

    http://mobiforge.com/developing/story/sms-messaging-android

    • September 1, 2010 3:14 am

      Yup if you look in my post you’ll see that I actually reference that site and call the hyperlink “Advanced Tutorial”.

    • deep permalink
      September 12, 2011 11:13 pm

      Gud very good…nice tutorial.///.

  3. Nouman Naseer permalink
    September 29, 2010 10:53 am

    Thanks mate… I worked around with my code using this example… not tested but I guess it should work 🙂

  4. Learner permalink
    March 30, 2011 10:15 pm

    Hi all,

    i m first time working on android mobiles,my application purpose is first to connect with android phone that is connected by the usb cable to the PC.than send sms by android phone using desktop VC++ MFC application.

    please help me from where i can start?
    and also tell me any example or links if possible.

    thanks in advance.

  5. ashish permalink
    April 18, 2011 1:07 am

    how to send sms without launching message compose page

    • May 2, 2011 1:34 am

      Hey ashish,

      Use the first method I proposed =)

      – jwei

      • bhargav permalink
        July 21, 2011 8:46 pm

        jwei

        thank for your great tutorials which are very easy to undertand.I just wanted to know how to send and receive text through the socket porgramming.Can I use the same code(the above code)in the method run(which have indicated you can be creative at this point) in the server class.

        Or

        can i sue the moboforge example sms sending and receiving classes in your socket programming example.

        THank you and hope you reply me soon.

  6. June 10, 2011 9:15 am

    Hey, thank you! You’re right– there is almost NO documentation about the NPE that an over-160 char message throws you. I thought I was going INSANE until I found your post.

    Keep up the good work!!!! Hooray for disclosure!!!!

  7. August 15, 2011 9:35 pm

    Gr8 Work !!!
    Cheers!!!

  8. randa permalink
    September 20, 2011 5:20 pm

    hey
    i think this about sending sms while the device is connected to the internet
    is there an way to send sms using the sim card only

    • October 16, 2011 6:46 pm

      Hey Randa,

      I believe these intent actions work without the internet… as long as you have regular network access for text-ing these should work.

      – jwei

  9. November 5, 2011 5:40 am

    its a nice thingy… :), cheers coders

  10. sachin permalink
    November 11, 2011 3:13 am

    this is simple and good way for sending sms thanks buddy.
    but i have one problem here when i send sms to 5556 that’s another emulator can i raise or show any notification to my 5554 emulator that sms is send to 5556 number.
    like sent notification.

  11. December 5, 2011 1:36 am

    Thanks a millions friend. Such a concise code.

  12. ilya permalink
    January 5, 2012 2:28 pm

    thanks

  13. Rethinavel permalink
    March 7, 2012 6:34 am

    It’s very simple yaar..! Thanks

  14. Sohaib Shaheen permalink
    May 8, 2012 11:17 am

    Couldn’t be simpler!! Best tutorial on sending SMS

  15. Shankar.K permalink
    July 16, 2012 12:30 am

    Hi Dude thanks for this post..
    its working fine..
    but i need when i press phone number it shld direct to contact in phone..

Trackbacks

  1. Sending SMS from Application « Vladimir Morozov

Leave a comment