Wednesday, 9 November 2011

Android: Programmatically sending (text) SMS messages

Quick and easy one:

  android.telephony.SmsManager shortMessageManager;
  String phoneNumber="1110001100";
  String message="test text message";

  shortMessageManager.sendTextMessage(phoneNumber, null, message, null, null);

Very simple, the second NULL parameter to sendTextMessage is the address of the service center. Set it to NULL to use the phone's default setting (recommended).

If you want to know if the message was sent successfully or failed, create a pending intent and pass it as the forth argument to sendTextMessage. If you want to be notified when the message is delivered, create another pending intent and pass it as the last argument to the same method.

Good luck!