86

I want to send an SMS via intent, but when I use this code, it redirects me to a wrong contact:

Intent intentt = new Intent(Intent.ACTION_VIEW);         
intentt.setData(Uri.parse("sms:"));
intentt.setType("vnd.android-dir/mms-sms");
intentt.putExtra(Intent.EXTRA_TEXT, "");
intentt.putExtra("address",  phone number);
context.startActivity(intentt);

Why?

Also, I know a way to follow SMS sending, but I do not know how code this:

Starting activity: Intent { 
   act=android.intent.action.SENDTO dat=smsto:%2B**XXXXXXXXXXXX** flg=0x14000000    
   cmp=com.android.mms/.ui.ComposeMessageActivity }

where XXXXXXXXXXXX is phone number.

Janusz
  • 187,060
  • 113
  • 301
  • 369
Ata
  • 12,126
  • 19
  • 63
  • 97

10 Answers10

101

I have developed this functionality from one Blog. There are 2 ways you can send SMS.

  1. Open native SMS composer
  2. write your message and send from your Android application

This is the code of 1st method.

Main.xml

<?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout  
        android:id="@+id/relativeLayout1"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        xmlns:android="http://schemas.android.com/apk/res/android">  

            <Button  
                android:id="@+id/btnSendSMS"  
               android:layout_height="wrap_content"  
               android:layout_width="wrap_content"  
               android:text="Send SMS"  
               android:layout_centerInParent="true"  
               android:onClick="sendSMS">  
           </Button>  
   </RelativeLayout>

Activity

public class SendSMSActivity extends Activity {  
     /** Called when the activity is first created. */  
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
      }  

     public void sendSMS(View v)  
     {  
         String number = "12346556";  // The number on which you want to send SMS  
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));  
     }  
    /* or 
     public void sendSMS(View v) 
      { 
     Uri uri = Uri.parse("smsto:12346556"); 
         Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
         it.putExtra("sms_body", "Here you can set the SMS text to be sent"); 
         startActivity(it); 
      } */  
 }

NOTE:- In this method, you don’t require SEND_SMS permission inside the AndroidManifest.xml file.

For 2nd method refer to this BLOG. You will find a good explanation from here.

Hope this will help you...

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Prem
  • 1,664
  • 3
  • 15
  • 20
  • 3
    One more thing, for testing purpose of this app you can open 2 Emulators; they have id like 5554 & 5555 or something like that. You use this as the number and test it. – Prem Mar 21 '12 at 05:05
  • This is the only working way of sending sms via intent from all devices and android versions. – Uniruddh Oct 02 '14 at 11:27
  • @I-droid: which method you are referring to? – Marco Altran Oct 03 '14 at 19:31
  • @MarcoAltran: `sendSMS(View v) ` – Uniruddh Oct 06 '14 at 05:43
  • @Prem It's best to include content from your source and then just add a reference to it. Links can be broken sooner or later and then out of sudden your answer will not contain adequate information – Farzan Jun 05 '16 at 12:41
  • Can the 2nd approach be used in a play store app without being the default app? see https://android-developers.googleblog.com/2018/10/providing-safe-and-secure-experience.html – CamHart May 18 '19 at 07:29
  • Can those methods detect if messages send out successfully? – Guokas Dec 03 '19 at 02:10
  • how can i send my own predefined sms text as well? – Prajwal Waingankar May 04 '21 at 05:30
62
Uri uri = Uri.parse("smsto:YOUR_SMS_NUMBER");   
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   
intent.putExtra("sms_body", "The SMS text");   
startActivity(intent);  
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Bao Le
  • 16,643
  • 9
  • 65
  • 68
  • 2
    This does not work on Android 4.0.3 (and better?). I have to use Intent.EXTRA_TEXT instead. Any idea why? – wojciii Sep 20 '14 at 18:02
  • 1
    Just tested this on a couple of devices, running Android 6.0.1 and 4.4.4: in all cases `"sms_body"` worked fine, `Intent.EXTRA_TEXT` did not work. – Jonik Aug 11 '16 at 12:24
37

Create the intent like this:

Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","your desired phoneNumber");         
smsIntent.putExtra("sms_body","your desired message");
smsIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
Neo
  • 3,546
  • 1
  • 24
  • 31
Zeeshan
  • 1,625
  • 17
  • 25
8

Try this code. It will work

Uri smsUri = Uri.parse("tel:123456");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms"); 
startActivity(intent);

Hope this will help you.

Jasonw
  • 5,054
  • 7
  • 43
  • 48
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • There seems to be something wrong I get the warning that setting the type will overwrite the set URI – Janusz Jul 16 '19 at 13:54
  • 2021, this doesn't work anymore.. it does not show the default SMS provider in the "Open with" selection. In particular google's default 'Messages' app does not appear in the selection – user1034912 Jun 04 '21 at 13:31
5

If you want a certain message, use this:

String phoneNo = "";//The phone number you want to text
String sms= "";//The message you want to text to the phone

Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phoneNo, null));
smsIntent.putExtra("sms_body",sms);
startActivity(smsIntent);
theeman05
  • 61
  • 1
  • 4
4
   Uri uriSms = Uri.parse("smsto:1234567899");   
   Intent intentSMS = new Intent(Intent.ACTION_SENDTO, uriSms);   
   intentSMS.putExtra("sms_body", "The SMS text");   
   startActivity(intentSMS); 
2
/**
 * Intent to Send SMS
 * 
 *
 * Extras:
 *
 * "subject"
 *      A string for the message subject (usually for MMS only).
 * "sms_body"
 *      A string for the text message.
 *  EXTRA_STREAM
 *      A Uri pointing to the image or video to attach.
 *
 *  For More Info:
 *  https://developer.android.com/guide/components/intents-common#SendMessage
 *
 * @param phoneNumber on which SMS to send
 * @param message text Message to send with SMS
 */
public void startSMSIntent(String phoneNumber, String message) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    // This ensures only SMS apps respond
    intent.setData(Uri.parse("smsto:"+phoneNumber));
    intent.putExtra("sms_body", message);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
1

Hope this is work, this is working in my app

SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);
Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78
  • 1
    Does this way need a speacial permission ? – Radon8472 Jun 02 '15 at 22:31
  • From documentation: _Using this method requires that your app has the SEND_SMS permission._ – shkschneider Jul 17 '15 at 12:59
  • This also only works if the message is 160 characters or less. If it's more than 160 you must use `sendMultipartTextMessage` instead of `sendTextMessage` – Randy Apr 07 '18 at 12:30
  • 3
    Sorry down voting, this method requires SEND SMS permissions and will not be accepted, Google will not allow publishing the app unless its a core SMS app. That means your app's primary functionality must be SMS messenger, there are few exceptions. However most app will not be approved and will be rejected during roll out. – MG Developer Dec 12 '19 at 01:58
1

Add try-catch otherwise phones without sim will crash.

void sentMessage(String msg) {
    try {
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        smsIntent.setType("vnd.android-dir/mms-sms");
        smsIntent.putExtra("sms_body", msg);
        startActivity(smsIntent);
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "No SIM Found", Toast.LENGTH_LONG).show();
    }
}
Shohan Ahmed Sijan
  • 4,391
  • 1
  • 33
  • 39
0
  1. Manifest permission (you can put it after or before "application" )
 uses-permission android:name="android.permission.SEND_SMS"/>
  1. make a button for example and write the below code ( as written before by Prem at this thread ) and replace the below phone_Number by an actual number, it will work:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "phone_Number", null)));
realr
  • 3,652
  • 6
  • 23
  • 34
Kerelos
  • 83
  • 4