Send SMS Example

This example shows how to send an SMS on a suitably equipped device.

A screenshot of the application.

The example only performs minimal checking to ensure that some sort of phone number is entered by the user and that the message is not empty. It does not check whether the message was sent successfully, nor does it receive a delivery notification.

We begin by importing the classes and modules needed by our application. The most relevant ones for this example are those from the android.telephony module.

from import Activity
from android.content import Context
from android.os import Build
from android.telephony import SmsManager
from android.text import InputType, TextWatcher
from android.view import View, ViewGroup
from android.widget import Button, EditText, LinearLayout, TextView

We import the the following module so that we can specify the theme of the application.

import android.R

We also use a convenience widget from the serpentine package.

from serpentine.widgets import VBox

We define a class based on the standard Activity class. This represents the application, and will be used to present a graphical interface to the user.

class SendSMSActivity(Activity):

    __interfaces__ = [TextWatcher, View.OnClickListener]

The initialisation method only needs to call the corresponding method in the base class.

    def __init__(self):

The onCreate method is called when the activity is created. Our implementation calls the onCreate method of the base class, queries the available telephony features and displays them in a graphical layout.

    def onCreate(self, bundle):
        Activity.onCreate(self, bundle)

We set the theme of the application to the device's default. This tends to make it fit in better with the rest of the user interface.


The user interface is a simple collection of text labels and editors that allow the user to enter a phone number and a message. The message is sent when the user clicks the "Send message" button.

        numberLabel = TextView(self)
        numberLabel.setText("Phone number:")
        self.numberEdit = EditText(self)
        textLabel = TextView(self)
        self.textEdit = EditText(self)
        self.sendButton = Button(self)
        self.sendButton.setText("Send message")

The widgets are added to a convenience widget from the serpentine package.

        vbox = VBox(self)

The VBox widget is used as the main user interface in the activity.


We implement the following three methods to implement the TextWatcher interface.

    def afterTextChanged(self, s):
    def beforeTextChanged(self, s, start, count, after):

The following method is the only one from the TextWatcher interface that we implement in full, checking the contents of the two editors in the user interface and enabling the send button if both of them contain text.

    def onTextChanged(self, s, start, before, count):
        if str(CAST(self.numberEdit, TextView).getText()) != "" and \
           str(CAST(self.textEdit, TextView).getText()) != "":

We implement the following method to implement the View.OnClickListener interface. The method is called when the user clicks the send button.

    def onClick(self, view):

We obtain the number and text that the user entered.

        number = str(CAST(self.numberEdit, TextView).getText())
        text = str(CAST(self.textEdit, TextView).getText())

Since this method can only be called when the number and text editors contain values, we can use those values with the default SmsManager to send a text message. Note that we do not check the validity of the number but we split the message into pieces that are short enough to send.

        smsManager = SmsManager.getDefault()
        for message in smsManager.divideMessage(text):
            smsManager.sendTextMessage(number, None, message, None, None)

Finally, we clear the editors so that the send button is disabled again.