How to send SMS programmatically from an Android application

Suyash Joshi
RingCentral Developers
3 min readMay 4, 2022

--

Wondering how to use the RingCentral Platform to send SMS programmatically from within an Android application? RingCentral provides a set of SMS APIs for business applications that allows you to do that. Let’s take a look at how we can leverage them in this simple yet effective Android application.

Setup:

  • Download / Clone the project source code from GitHub
  • Open the file “SendSMS.java” and update the constant fields with your RingCentral application sandbox or production credentials. You will also need to provide JWT Credential, refer to this guide on how to create/access the same.
  • Run the project from Android Studio on a device or emulator.
  • Enter a valid phone number that can receive SMS and a standard text message and hit ‘Send’. Confirm the message on the recipient's device! If you’re using Sandbox credentials, the message will be prefixed with the following text “Test SMS using RingCentral Developer account”.

Key Components of the Application:

  • Module’s build.gradle file : This is how you install the RingCentral Java SDK and call the SMS APIs.
dependencies {
implementation 'com.ringcentral:ringcentral:2.8.4'

....
}
  • View Logic in activity_main.xml & MainActivity.java: The application has just a single activity that will look as seen below in the screenshot. It accepts user input — Recipient’s Phone Number & Text Message(limited to 160 characters) and performs the ‘Send SMS’ action, the outcome of which will be displayed as a Toast Message.
public class MainActivity extends AppCompatActivity implements Executor {

// When Send SMS button is pressed, create an SendSMS Object and pass it the number and text message
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("Main Activity", "Listening for send press to send the SMS");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button) findViewById(R.id.btnSendSMS);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Context context = getApplicationContext();
EditText numberEditText = (EditText) findViewById((R.id.phoneNumber));
String recipientNumber = numberEditText.getText().toString();
EditText messageEditText = (EditText) findViewById(R.id.smsMessage);
String messageContent = messageEditText.getText().toString();
SendSMS sendSMS = new SendSMS(recipientNumber, messageContent);
execute(sendSMS);
// Show Toast
Toast.makeText(context, "SMS Sent!", Toast.LENGTH_SHORT).show();
// Clear out fields after sms has been sent
numberEditText.getText().clear();
messageEditText.getText().clear();
}
});
}

// Perform executing of the run() method from SendSMS Class in a new thread
@Override
public void execute(Runnable runnable) {
new Thread(runnable).start();
}
}
  • Business Logic in SendSMS.java : The logic of how to set up the RingCentral platform authorization and invocation of Creating and Sending Message can be found there. The network operation (HTTP POST Send SMS) is done with the help of the Executor object that uses the Runnable object and performs the operation in a separate thread so as to not lock up the main thread in the application.
  // Business logic of sending the SMS using RingCentral SendSMS API
public void run() {
RestClient rc = new RestClient(RINGCENTRAL_CLIENT_ID, RINGCENTRAL_CLIENT_SECRET, RINGCENTRAL_SERVER_URL);
try {
rc.authorize(RINGCENTRAL_JWT);
CreateSMSMessage postParameters = new CreateSMSMessage();
postParameters.from = new MessageStoreCallerInfoRequest().phoneNumber(RINGCENTRAL_DEV_NUMBER);
postParameters.to = new MessageStoreCallerInfoRequest[]{
new MessageStoreCallerInfoRequest().phoneNumber(recipientNumber)
};
postParameters.text = smsMessage;
GetSMSMessageInfoResponse response = rc.restapi().account().extension().sms().post(postParameters);
Log.i("SendSMS", "SMS sent. Message status: " + response.messageStatus);
}
catch (RestException | IOException e) {
e.printStackTrace();
Log.e("Error Occurred", e.getMessage());
}
}

In this article, we learned how we can use RingCentral APIs in Android applications by using the Java SDK, which allows you to easily call any of the platform APIs. Please let us know what you think by leaving your questions and comments below. To learn even more about other features we have make sure to visit our developer site and if you’re ever stuck make sure to go to our developer forum.

Want to stay up to date and in the know about new APIs and features? Join our Game Changer Program and earn great rewards for building your skills and learning more about RingCentral!

References:

  1. App and Code on GitHub : https://github.com/suyashjoshi/RingCentralSMS-Android
  2. RingCentral SMS FAQ : https://support.ringcentral.com/article/RingCentral-Business-SMS-Frequently-Asked-Questions.html
  3. Send SMS API : https://developers.ringcentral.com/api-reference/SMS/createSMSMessage
  4. Java SDK : https://search.maven.org/artifact/com.ringcentral/ringcentral/2.8.4/jar
  5. Android Executor : https://developer.android.com/reference/java/util/concurrent/Executor

--

--