Sending a single SMS

Sending SMSs on Android is more than simple. We add the following permission to the manifest file:

    <uses-permission android: name = “android.permission.SEND_SMS” />

And then we go to code:

SmsManager is simply a tool for sending SMS. The meaning of the other 2 variables – recipient and message are not difficult to guess. Let’s now look at lines 22 and 23. The getDefault method gives us an instance of the SmsManager class. The sendTextMessage method is used to send SMS of no more than 160 characters – ie a single SMS. There is also a “sendMultipartTextMessage” method that allows you to send the text to more than one SMS. The first parameter of the sendTextMessage method is the recipient’s phone number, the third is the message body. We must remember that the device from which we send messages must have a SIM card

Sending multi-part SMS

Sending a multipart SMS is very similar to sending a single message, with the difference that the message must be divided into several passages on the way, but even for that, we have a dedicated method. Let’s start by adding the necessary permission:

<uses-permission android: name = “android.permission.SEND_SMS” />

Then we go to the code:

The SmsManager class is an element that sends SMS. The recipient variable is the phone number for which we want to send the message. List of elements, defined as empty, is a container to which an SMS message is divided into fragments. A variable is a temporary message that is longer than 160 characters. In the onCreate method, on line 28 I receive an instance of the SmsManager class. In line 29, I use the divideMessage method, which I am going to send a long message to, and this returns it to the fragments we have already split into fragments. The only way to send a message is to call the sendMultipartTextMessage method, where I give the recipient’s phone number and the message divided into fragments. Along the way (on line 30) I display the number of elements that will come out of our message.
The message comes as a whole. Of course, we remember to insert a SIM card into the device from which we want to send sms, because contrary to appearances is not a method forSMSee text messages.;)
Receiving SMS
If we want to write an application that will respond to an incoming SMS, we need to register it as such a receiver and thus declare that it can handle such an action. To do this, somewhere between the <Application> tags in the manifest file, we need to insert the following:
<receiver android: name = “.SMS receiver” android: enabled = “true”>
         <Intent-filter>
             <action android: name = “android.provider.Telephony.SMS_RECEIVED” />
             <category android: name = “android.intent.category.DEFAULT” />
         </ Intent-filter>
     </ Receiver>
Of course, in the first line we give the name of our class which has sesemes to receive. By inserting this piece of code into our manifest file, we declare that our program is capable of handling some operations – for example, in this case android.provider.Telephony.SMS_RECEIVED, but it could also be incoming incoming calls or opening web page calls.
Since we declare that we are able to handle SMS reception, we still have to have permission to do ;)so. So we have to add this line to the manifest file:
  <uses-permission android: name = “android.permission.RECEIVE_SMS” />
An hourkeeper who will handle it:
We need to implement the BroadcastReceiver interface and have an onReceive method in which we determine what is going to happen when receiving the sms. You do not have to worry that our application will now “cover” the default SMS handling. The Android platform at the moment of receiving the SMS will call all applications registered as SMS receivers 🙂
This functionality can be used to create a managed system from anywhere on the earth via SMS