0

I'm trying to write an app that sends SMS messages. To do this, I use the following code:

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("sms:" + number));
smsIntent.putExtra("sms_body", message);
smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);

The problem is: when I run it, it shows me a dialog and I need to choose with which app I want to send the message (WhatsApp/Hangouts/Messaging/etc.) and when I choose "Messaging", it only prepares the message and waits for me to press "send". How can I send the message immediately through "Messaging" (without the dialog and without waiting until "send" is pressed)?

SharonKo
  • 619
  • 2
  • 6
  • 19
  • possible duplicate of [Sending text messages programmatically in android](http://stackoverflow.com/questions/8578689/sending-text-messages-programmatically-in-android) – Selvin Oct 09 '14 at 10:28

1 Answers1

0

try to use SmsManager API

    String phoneNo = "123456";
    String msg= "Hello";    
    buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

              try {

                   // send the message
                   SmsManager smsManager = SmsManager.getDefault();
                   smsManager.sendTextMessage(phoneNo, null, msg, null, null);

                  } catch (Exception e) {
                      e.printStackTrace();
                  }

            }
        });

add this to your manifest :

<uses-permission android:name="android.permission.SEND_SMS" />
Zied R.
  • 4,964
  • 2
  • 36
  • 67
  • Thanks, it sends the message but there is no record of the message in the "Messaging" window... is there a way to make it appear there? – SharonKo Oct 09 '14 at 13:20