The Android operating system runs on mobile computing devices such as smartphones. Android application programmers can utilise various aspects of these mobile devices, such as telephony functions. With the telephony resources in the Android APIs (Application Programming Interfaces), developers can write apps in which the user can send SMS text messages. If you are writing an Android app and wish to allow the user to send a text message, you can use the telephony classes directly within your Java code, sending an SMS with only a few lines of code.
- Skill level:
Other People Are Reading
Alter your application Manifest. To send SMS text messages, your app must have a particular permission. This will allow anyone downloading the app to see clearly that it has the facility to send SMS. Open your application Manifest file XML code. Add the following permission element:
Make sure you include the permission inside the manifest element but outside any other elements. Without this permission, the app will be unable to send text messages.
Add the telephony resources to your app. Open the Activity or other class you plan on sending the text message from. At the top of the class file, add the following import statement:
Create and instantiate an SMS Manager object at the point in your class where you wish to use SMS functionality:
SmsManager smsMan = SmsManager.getDefault();
This object will allow you to send text messages.
Configure the details of your SMS. The SMS Manager class provides methods for sending text messages. You pass the details of the message you want to send when you call these methods. You can include the address to send the message to, which can be a telephone number, the source address, which is the service centre address, the text content of the message, an Intent to broadcast when the message is sent and an Intent to broadcast when it is delivered. In many cases you will not need all of these details. The following sample code demonstrates creating variables for the recipient number and the text message content:
String recipientNum = "01234567890"; String textMessage = "Hi! How are you?";
Send the message. You can call the SMS Manager methods to send the message, passing the message details at the same time. The following example demonstrates:
smsMan.sendTextMessage(recipientNum, null, textMessage, null, null);
This code passes the recipient number and message content, leaving the source address and two Intents for when the message is sent or received null, as these are typically not required.
Tips and warnings
- You can allow users to share content to an application of their choice using the "ACTION_SEND" Intent rather than sending exclusively to the text messaging service.
- The appearance of the SMS sending permission may stop some users from downloading your app if sending text messages is not a vital part of the app's functionality.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for