How to send SMS in Android programming and how to configure it?
12 minute(s) read
Published on: May 11, 2021
Updated on: Dec 14, 2021
In this part of the training, we want to know how to send an SMS and how to find out if the SIM-card is active or not, receive a report on sending a text-message, and so on. If you want to know how to send SMS or its delivery reports, it is recommended that you follow this tutorial article.
Here's what you will learn in this tutorial:
- What are the uses of sending SMS through the application ?
- How to send SMS in Android programming - How to send via Intents
- How to use the Manager class to send
- Build an Android project to send on Android development - Define and receive Permission to send
- How to implement the Manager class
- How to receive and receive reports
- How to find out if the SIM-card is active or not
What are the uses of sending through the application?
Sending text-messages through applications has many uses. Among these applications, it can be mentioned that users can choose as their source of information when using mobile banks. In this case and mobile banks, if users can choose to receive information, all information about them will be sent to them via SMS.
Another application of sending in Android applications is that users can use SMS in Android systems to send their geographical location via a number that they have already defined. In this case, users already select a number, and with the touch of a button, their geographical location will be sent to that number via SMS. How to send in Android dev Sending text messages in Android systems can be done in two ways. In the first method, which is the use of intent, to send a text message, the user must be connected to the device's default application management program.
But in the second method and sending a text-message using the SMS Manager class, the text message will be sent in the program itself, and the user will not need any program to manage the message. It should be noted that it will not be possible to edit the SMS and access its content.
How to send-via-Intents
The codes related to sending SMS via Intents are as follows:
By entering the above codes, the user is transferred to the default SMS program of the device, and it is also worth mentioning that the message recipient number and message text are added automatically.
In this case, the user can see it before sending the message and edit it if necessary.
How to use the SMS Manager class to send
In this method of sending, SMS are sent to the destination within the applications themselves, and the user will not have any access to the SMS content and will not be able to manage or edit it. It should be noted that the user will not be informed about the SMS sending process.
This method can be used to send regular text-messages. In other words, they are sending a text message in the form of a message to a predetermined destination.
The function and operation of this method are similar to the previous method, except that this method can send the same message in the form of several messages.
This method can be used to send data-driven messages.
This method is also used to send multimedia messages.
In this part of the tutorial, we want to create a project that can send a simple message that the sendTextMessage method is the most suitable method to do this.
Build an Android project to send on Android apps
1- We create a new project in Android Studio and choose its name as desired. The name chosen in this tutorial is Send.
2- Select the type of activity from the Empty Activity type.
3- The language used in this part of the tutorial is Java. Be sure to select the Java language.
4- In this project, we want to use the SmsManager class and send a short message to the pre-specified destination without a text-message management program.
5- Convert the TextView located in the Constraint Layout center in the project layout to a Button.
6- We must assign an id to the created button.
7- The codes that should be in the activity_main.xml section are as follows:
8- We need to specify the events related to this created button.
9- The codes that should be in the MainActivity.java section are as follows:
What is the manager in android?
How to implement the SmsManager class?
10- We must define the Permission related to sending SMS in the program.
11- First, we need to define the SEND_SMS Permission in the program manifest part.
12- The codes that should be in the AndroidManifest.xml section are as follows:
Note: To get a license to send SMS on Android systems with version 6 and above, we must do the following and use the codes listed below.
13- The codes that should be in the MainActivity.java section are as follows:
How to implement the SmsManager class
After we add the codes listed to the program and project, if we touch the "Send SMS" button and the Permission to send SMS is given, the send Message method will be called.
14- We need to add a function to the activity after the onCreate method.
15- hen, add the codes listed below.
For the program not to crash in the codes listed, we have put all the codes related to sending SMS in a try-catch. In other words, we can say that we put the codes related to sending SMS in the try-catch to prevent the program from crashing when it does not run for some reason.
In the above code, we first create an instance of the manager class with the custom name SmsManager. We use the get Default method to set it. It should be noted that this method is used to get the default and desired device SIM-card ID that the device SIM-card ID is required to send a text message.
Then we define the sendTextMessage method, which has four input parameters:
The first parameter of this method is the type String and is used to maintain the recipient of sending SMS. The recipient number is kept in this parameter with a String type.
The second parameter is the method for maintaining the address or service center of the operator whose user uses the SIM-card to send a text message. Because the value of this parameter must be entered by the device and the system and not by the user, we consider the value null.
The third parameter is this method that is used to store the textual content of the SMS.
Sent Intent and delivery Intent: are the parameters used first to receive the sending report and the second to report receiving the SMS. We consider the value null for both of them.
16- We run the project to check the performance of SmsManager.
17- After running the project, if we touch the "Send SMS" button, we will be faced with Permission to send SMS.
18- For a text message to be sent, we must confirm that it will be sent after the Permission to send it is displayed.
19- After confirming the sending of the SMS, the message "SMS sent" will be displayed.
20- It is worth mentioning that if we check the default SMS management program on the device, we will see that there is also a text message there.
How to receive reports Receive and send-SMS
In the previous section, we were only able to provide settings that send an SMS using the SmsManager class, and we did not receive any sent or received reports. Therefore, in this part of the training, we must apply the settings so that we can also receive reports of sending and receiving SMS.
In the previous section, we set the value null for the sent Intent and delivery Intent parameters, which meant that we did not receive any sent or received reports from the user.
In this part of the tutorial, we want to use BroadcastReceiver and PendingIntent and make settings related to them to display reports related to sending and receiving messages to the user.
21- To do this, we define two variables of type String within the activity with arbitrary names.
22- It should be noted that we use these variables to communicate between BroadcastReceiver and PendingIntent.
23- Then, we have to register the two broadcasts inside the send Message mat and in the try block before SmsManager. Like the following:
In the codes listed, the first Broadcast will be used to hear the status of the text message. If the SMS is sent correctly and without any problems to the operator's server, the message "SMS sent" will be displayed to the user.
In the codes listed, four different modes are provided for not sending SMS.
Depending on the situation received from SmsManager, the type of error can be specified and presented to the user. These errors can be a general error, an operator unavailability error, a SIM-card unavailability error, or a PDU.
In the codes listed, the second Broadcast is also used to listen to the delivery or non-delivery status of SMS.
24- After broadcasts, we must define two PendingIntent. Like the following:
- PendingIntent Send-SMS =
PendingIntent.getBroadcast (this, 0, new Intent (SMS_SENT), 0);
- PendingIntent delivers MS =
PendingIntent.get broadcast (this, 0, new Intent (SMS_DELIVERED), 0);
If we use the get Broadcast method, the desired Broadcast will be defined in PendingIntent.
The third parameter of getting Broadcast is the Internet. It should be noted that this parameter includes SMS_SENT and SMS_DELIVERED actions. The first is to send a text message to run Broadcast and the second is to run a text message to run a broadcast.
25- Then, we have to replace the two null PendingIntent in SmsManager with null values.
By inserting the above codes by sending an SMS by SmsManager, whenever the report of sending and delivery is published, they are received by BroadcastReceiver, the appropriate message is displayed.
26- Run the project and touch the "Send SMS" button.
27- After running the program, we will see that the message related to sending and receiving the program is displayed for the user at the appropriate time.
28- The final and complete code that should be in the MainActivity.java section is as follows:
How to find out if the SIM card is active or not
We can use the TelephonyManager classes and the getSimState method to check the status of the SIM card before sending the SMS and perform our desired operations accordingly. For example, the user will be informed about the status of the SIM card, and if the SIM card is inactive, the "Send SMS" button will be disabled to inform the user, and the user will be informed that the SIM card is inactive.
29- Define a method inside activity and name it as simulate and then complete it as follows.
In the codes listed, the status of the SIM card is checked using getSimState, and if the SIM card is available, the SIM_STATE_READY mode is returned.
30- We run the project in two modes, whether the SIM card is active or not, and check the results.
31- The complete code that should be in the MainActivity.java section is as follows:
In this part of the tutorial, we sent a simple text-message and made the relevant settings.
Here is what we have taught and learned in this tutorial:
- What are the uses of sending SMS through the application ?
- How to send in Android systems - How to send via Intents
- How to use the SmsManager class to send
- Build an Android project to send on Android systems
- Define and receive Permission to send
- How to implement the SmsManager class
- How to receive and receive SMS reports
- How to find out if the SIM card is active or notAbout our Android and iOS app development services