33

I'm rather new to Android. Im trying to send SMS from Android application. When using the SMS Intent the SMS window opens and the user needs to approve the SMS and send it.

Is there a way to automatically send the SMS without the user confirming it?

Thanks, Lior

Saravanan
  • 363
  • 1
  • 14
user733284
  • 925
  • 2
  • 11
  • 18

4 Answers4

40

You can use this method to send an sms. If the sms is greater than 160 character then sendMultipartTextMessage is used.

private void sendSms(String phonenumber,String message, boolean isBinary)
{
    SmsManager manager = SmsManager.getDefault();

    PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
    PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);

    if(isBinary)
    {
            byte[] data = new byte[message.length()];

            for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
            {
                    data[index] = (byte)message.charAt(index);
            }

            manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);
    }
    else
    {
            int length = message.length();

            if(length > MAX_SMS_MESSAGE_LENGTH)
            {
                    ArrayList<String> messagelist = manager.divideMessage(message);

                    manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
            }
            else
            {
                    manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
            }
    }
}

Update

piSend and piDelivered are Pending Intent They can trigger a broadcast when the method finish sending an SMS

Here is sample code for broadcast receiver

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String message = null;

            switch (getResultCode()) {
            case Activity.RESULT_OK:
                message = "Message sent!";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                message = "Error. Message not sent.";
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                message = "Error: No service.";
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                message = "Error: Null PDU.";
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                message = "Error: Radio off.";
                break;
            }

            AppMsg.makeText(SendMessagesWindow.this, message,
                    AppMsg.STYLE_CONFIRM).setLayoutGravity(Gravity.BOTTOM)
                    .show();
      }
  };

and you can register it using below line in your Activity

registerReceiver(receiver, new IntentFilter(SMS_SENT));  // SMS_SENT is a constant

Also don't forget to unregister broadcast in onDestroy

@Override
protected void onDestroy() {
    unregisterReceiver(receiver);
    super.onDestroy();
}
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • In a dual sim phone, the sim card to send message is selected for the user in dual sim settings ( android general settings ) in preferred sim settings. Is possible select what sim uses to send sms (Programmatic) ? – richie-torres Jul 01 '13 at 16:37
  • @AndrésRicardoTorresMartínez Dual sim is not supported by Android. It is supported by device manufactures who wrote their own API's for supporting dual sim. So please consult the device manufactures for this. – Sunny Jul 02 '13 at 08:58
  • How to check if the message was successfully sent? Once the text is sent, will it show up on my text messaging app? Also, what if I want to send to multiple phone numbers? – Si8 Oct 09 '13 at 14:54
  • So call `sendSMS` function and right below that add `registerReceiver(receiver, new IntentFilter(SMS_SENT));` from `main`? What do you mean by`SMS_SENT` is constant? Can I also do a recursion, that if the message fails to send, to retry every hour? Thank you :) – Si8 Oct 09 '13 at 15:23
  • 1
    Yes call sendSMS function and right below that add registerReceiver(receiver, new IntentFilter(SMS_SENT)); If you want to send SMS every hour you can use `AlaramManager` class. You can call this method any number of times :) . SMS_SENT mean it is just a constant like `private static final String SMS_SENT = "my.app";`. – Sunny Oct 09 '13 at 15:29
  • hi can you please tell me how do i send sms when accelerometer detect a fall – Abhishek Feb 24 '16 at 08:22
  • @Sunny After sending sms i am not getting broadcat even i have already register it. Could you tell me why? – nAkhmedov Mar 31 '16 at 06:33
  • @Sunny Sorry for distrubing! i have registered receiver via LocalBroadcastManager.getInstance(context). It was wrong. Your example is working on. Thanks – nAkhmedov Mar 31 '16 at 13:09
  • @Sunny Awesome Answer but what is `SMS_PORT` thanks :) – KISHORE_ZE May 03 '16 at 18:13
  • what is the value of MAX_SMS_MESSAGE_LENGTH 140 or 160 ? – Prashanth Debbadwar Aug 04 '16 at 12:48
  • How quickly will I get the confirmation of the msg sent? I have where the user can retry the msg and I have to check whether the msg was delivered or not? – Aman Verma Dec 19 '18 at 22:15
  • you can wait until that broadcast finish. It may take a second or so may be less. – Sunny Dec 24 '18 at 12:29
19

If your application has in the AndroidManifest.xml the following permission

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

you can send as many SMS as you want with

SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(...);

and that is all.

Filipe Pinheiro
  • 1,082
  • 8
  • 32
7

Yes, you can send SMS using the SmsManager. Please keep in mind that your application will need the SEND_SMS permission for this to work.

Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
3

Yes, you can send sms without making user interaction...But it works, when user wants to send sms only to a single number.

try {    
    SmsManager.getDefault().sendTextMessage(RecipientNumber, null,    
    "Hello SMS!", null, null);    
} catch (Exception e) {    
    AlertDialog.Builder alertDialogBuilder = new    
    AlertDialog.Builder(this);    
    AlertDialog dialog = alertDialogBuilder.create();    
    dialog.setMessage(e.getMessage());    
    dialog.show();    
}    

Also, add manifest permission....

<uses-permission android:name="android.permission.SEND_SMS"/>
Nikhil
  • 1,212
  • 13
  • 30