156

I have a question about an intent... I try to launch the sms app...

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType("vnd.android-dir/mms-sms");
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
    Intent.FLAG_ACTIVITY_CLEAR_TOP;
intent.setFlags(flags);
intent.setData(Uri.parse("content://sms/inbox"));
context.startActivity(intent);

so, you can see that I put too much things in my intent, but that's because I don't know how I can do... Thank's

harshit
  • 3,788
  • 3
  • 31
  • 54
Olivier69
  • 1,561
  • 2
  • 10
  • 4

22 Answers22

247

To start launch the sms activity all you need is this:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);         
sendIntent.setData(Uri.parse("sms:"));

You can add extras to populate your own message and such like this

sendIntent.putExtra("sms_body", x); 

then just startActivity with the intent.

startActivity(sendIntent);
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
jqpubliq
  • 11,874
  • 2
  • 34
  • 26
  • I tried, but when I write exactly what you give, eclipse console return (when compiling) "No Launcher activity found!" – Olivier69 Mar 03 '10 at 16:07
  • okay, do you have an activity with the following attributes in its intent filter in your manifest? – jqpubliq Mar 03 '10 at 16:26
  • in fact, the launch come from an appwidgetactivity. Perh'aps it coms from here, I put what you've done in the manifest and nothing happen... I have test my button with an other function et this one don't want to go!! – Olivier69 Mar 03 '10 at 16:41
  • I correct myself, I am in a widget, logcat want me to put FLAG_ACTIVITY_NEW_TASK flag... but I don't no where, can you help me? – Olivier69 Mar 04 '10 at 17:45
  • Sorry, I understood what I have to write: Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sendIntent.setData(Uri.parse("sms:")); context.startActivity(sendIntent); But "sms:" permit me to write a new sms but I want to show sms/inbox. please help! – Olivier69 Mar 04 '10 at 18:31
  • that I dont really know. My suggestion would be to check around to see if you can find the package or intent type to call. The mms client might be part of the available source. – jqpubliq Mar 04 '10 at 22:46
  • 25
    to auto include phone number in the 'to' box, change setData line of code to `sendIntent.setData(Uri.parse("sms:" + phoneNumber));` (thank you jqpubliq and jaydeepw) – tmr Oct 23 '14 at 00:25
  • can you know how can we change the font of the text in the sms? the font .ttf file is saved in my sdcard – jyomin Jul 20 '15 at 12:41
  • 2
    i need to check SMS is sent or not when user will send sms from my app through Intent ti sms how i can do this ? – user Aug 27 '15 at 20:22
  • If no apps shows up, you may need add `sendIntent.putExtra("address", "");` – jt-gilkeson Apr 15 '17 at 00:40
143
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
Elad Gelman
  • 1,182
  • 1
  • 13
  • 27
Pratik Sharma
  • 1,435
  • 1
  • 9
  • 3
50

If android version is Kitkat or above, users can change default sms application. This method will get default sms app and start default sms app.

private void sendSMS() {    
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
      {
         String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); // Need to change the build to API 19

         Intent sendIntent = new Intent(Intent.ACTION_SEND);
         sendIntent.setType("text/plain");
         sendIntent.putExtra(Intent.EXTRA_TEXT, "text");

         if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
         // any app that support this intent.
         {
            sendIntent.setPackage(defaultSmsPackageName);
         }
         startActivity(sendIntent);

      }
      else // For early versions, do what worked for you before.
      {
         Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
         smsIntent.setType("vnd.android-dir/mms-sms");
         smsIntent.putExtra("address","phoneNumber");         
         smsIntent.putExtra("sms_body","message");
         startActivity(smsIntent);
      }
   }
Sreejith B Naick
  • 1,203
  • 7
  • 13
msevgi
  • 4,828
  • 2
  • 24
  • 30
  • 2
    I am using KitKat version. I want to set the receiver number in To field. How can I set that ? – Karthikeyan Ve Mar 12 '15 at 10:13
  • 3
    @KarthikeyanVe please can you try? Intent sendIntent = new Intent(Intent.ACTION_SENDTO); sendIntent.setData(Uri.parse("smsto:phonenumber")); – msevgi Mar 12 '15 at 11:21
  • I tried. Its working fine. But when I click the back button from the message app, it also closes my app. If I back pressed from message app, it should back to my app where I left. – Karthikeyan Ve Mar 12 '15 at 11:55
  • @KarthikeyanVe, i have no idea, i will search. – msevgi Mar 12 '15 at 12:20
  • This does not work for android 5.0. I wish android would just fix this, these intents from day one have had issues. – JPM Mar 26 '15 at 19:30
  • @JPM please can you post logcat? – msevgi Mar 26 '15 at 20:25
  • Nothing really to post, since it doesn't give you a list of Intents just the default selected one. I have two Messengers on my phone and want to see a list of Activities for me to choose. – JPM Mar 27 '15 at 18:33
  • 2
    Great answer. Use if you want to go directly to the default SMS app, works like charm. If you want chooser see other answers here. – Nemanja Kovacevic Sep 17 '15 at 20:31
  • @JPM the reason you dont see the list of sms apps is that this methode was intended not to show the list, since 5.0.1 is higher than kit kat, then it will just open the default sms app – Amin Keshavarzian Apr 16 '16 at 06:47
  • Instead of sendIntent.putExtra(Intent.EXTRA_TEXT, "text"); to send sms body, sendIntent.putExtra("sms_body", "text"); worked for me. – SamFast Jun 22 '16 at 16:52
  • 1
    why app close when i come back from message – Muhammad Younas Oct 22 '16 at 07:57
32
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);

That's all you need.

skyisle
  • 576
  • 8
  • 11
27

If you want to launch SMS Composing activity from some of your other activity and you also have to pass a phone number and SMS text, then use this code:

Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx"); 
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri); 
sms_intent.putExtra("sms_body", "Good Morning ! how r U ?"); 
startActivity(sms_intent); 

Note: here the sms_body and smsto: is keys for recognizing the text and phone no at SMS compose activity, so be careful here.

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
18

Here is the code that will open the SMS activity pre-populated with the phone number to which the SMS has to be sent. This works fine on emulator as well as the device.

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber));
startActivity(smsIntent);
Mushfiqur Rahman
  • 306
  • 4
  • 18
JaydeepW
  • 3,237
  • 1
  • 25
  • 27
6

In kotlin this can be implemented easily as follows:

/**
 * If android version is Kitkat or above, users can change default sms application.
 * This method will get default sms app and start default sms app.
 */
private fun openSMS() {
    val message = "message here"
    val phone = "255754......." //255 Tanzania code.

    val uri = Uri.parse("smsto:+$phone")
    val intent = Intent(Intent.ACTION_SENDTO, uri)

    with(intent) {
        putExtra("address", "+$phone")
        putExtra("sms_body", message)
    }

    when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT -> {
            //Getting the default sms app.
            val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context)

            // Can be null in case that there is no default, then the user would be able to choose
            // any app that support this intent.
            if (defaultSmsPackageName != null) intent.setPackage(defaultSmsPackageName)

            startActivity(intent)
        }
        else -> startActivity(intent)
    }
}

This is modified answer of @mustafasevgi

Nux
  • 5,890
  • 13
  • 48
  • 74
5

Use

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName("com.android.mms", "com.android.mms.ui.ConversationList");
Harinder
  • 11,776
  • 16
  • 70
  • 126
4
Intent eventIntentMessage =getPackageManager()
 .getLaunchIntentForPackage(Telephony.Sms.getDefaultSmsPackage(getApplicationContext));
startActivity(eventIntentMessage);
Anoop
  • 23,044
  • 10
  • 62
  • 76
Vinayak V Naik
  • 1,291
  • 1
  • 9
  • 7
  • For SDK_INT>=19 (where this api is available) it works. In fact is the only working solution in my Nexus 6 with Nougat if you want to open sms app with list of messages – lujop Sep 05 '16 at 14:44
4

I use:

Intent sendIntent = new Intent(Intent.ACTION_MAIN);
sendIntent.putExtra("sms_body", "text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Fakhar
  • 3,946
  • 39
  • 35
Delgado
  • 41
  • 1
3
try {
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setData(Uri.parse("smsto:" + Uri.encode(number)));
    smsIntent.putExtra("address", number);
    smsIntent.putExtra("sms_body", message);

    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> resInfo = pm.queryIntentActivities(smsIntent, 0);

    for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;

        if (packageName.contains("sms")) {
            //Log.d("TAG", packageName + " : " + ri.activityInfo.name);
            smsIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        }
    }
    activity.startActivity(smsIntent);
} catch (Exception e) {
    // Handle Error
}

Best way of doing this.

BuffMcBigHuge
  • 579
  • 5
  • 8
3

You can open default sms App and pass details as below :
Note : If u want to send to many numbers, separate each number with ";" inside string

String mblNumVar = "9876543210;9123456789";
Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
smsMsgAppVar.setData(Uri.parse("sms:" +  mblNumVar));
smsMsgAppVar.putExtra("sms_body", "Hello Msg Tst Txt");
startActivity(smsMsgAppVar);

|Or| Use this function :

void openSmsMsgAppFnc(String mblNumVar, String smsMsgVar)
{
    Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
    smsMsgAppVar.setData(Uri.parse("sms:" +  mblNumVar));
    smsMsgAppVar.putExtra("sms_body", smsMsgVar);
    startActivity(smsMsgAppVar); 
}
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
  • This shows two options, 1. hangout and 2. default message app. I want to forcefully open Android messages app. How can we archive that? – Milind Mevada Dec 18 '17 at 18:12
2
Intent sendIntent = new Intent(Intent.ACTION_SEND); 
//CHANGE YOUR MESSAGING ACTIVITY HERE IF REQUIRED 
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body",msgbody); 
sendIntent.putExtra("address",phonenumber);
//FOR MMS
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mms.png"));
sendIntent.setType("image/png");
startActivity(sendIntent);
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
2

The best code that works with Default SMS app is.

Uri SMS_URI = Uri.parse("smsto:+92324502****"); //Replace the phone number
Intent sms = new Intent(Intent.ACTION_VIEW,SMS_URI);    
sms.putExtra("sms_body","This is test message"); //Replace the message witha a vairable 
startActivity(sms);
Junaid Khan
  • 520
  • 6
  • 15
2

For Kotlin simply do that:

val messageIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:03xxxxxxxxx"))
    messageIntent.putExtra("sms_body", "Enter your body here")
    startActivity(messageIntent)
Ghayas
  • 1,266
  • 10
  • 17
1

Compose SMS :

Uri smsUri = Uri.parse("tel:" + to);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("address", to);
intent.putExtra("sms_body", message);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
1

The below code works on android 6.0.
It will open the search activity in the default messaging application with the conversations related to specific string provided.

Intent smsIntent = new Intent(Intent.ACTION_MAIN);
        smsIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        smsIntent.setClassName("com.android.mms", "com.android.mms.ui.SearchActivity");
        smsIntent.putExtra("intent_extra_data_key", "string_to_search_for");
        startActivity(smsIntent);  

You can start the search activity with an intent. This will open the search activity of the default messaging application. Now, to show a list of specific conversations in the search activity, you can provide the search string as string extra with the key as

"intent_extra_data_key"

as is shown in the onCreate of this class

String searchStringParameter = getIntent().getStringExtra(SearchManager.QUERY);
    if (searchStringParameter == null) {
        searchStringParameter = getIntent().getStringExtra("intent_extra_data_key" /*SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA*/);
    }
    final String searchString = searchStringParameter != null ? searchStringParameter.trim() : searchStringParameter;

You can also pass the SENDER_ADDRESS of the sms as string extra, which will list out all the conversations with that specific sender address.

Check com.android.mms.ui.SearchActivity for more information

You can also check this answer

Community
  • 1
  • 1
sissyphus_69
  • 350
  • 5
  • 18
0

on emulator this work for me

Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null));
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("sms_body", remindingReason);

                startActivity(i);
0

Sms Intent :

Intent intent = new Intent("android.intent.action.VIEW");
        /** creates an sms uri */
        Uri data = Uri.parse("sms:");
        intent.setData(data);
Mitul Goti
  • 2,657
  • 1
  • 22
  • 19
0

Either you can use this ,both are working fine on android above 4.4

    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address","9212107320");
    smsIntent.putExtra("sms_body","Body of Message");
    startActivity(smsIntent);

or

    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.putExtra("sms_body","Body of Message");
    smsIntent.setData(Uri.parse("sms:9212107320"));
    startActivity(smsIntent);
0

You can use the following code snippet to achieve your goal:

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:"+model.getPhoneNo().trim()));
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.putExtra("sms_body","Hello this is dummy text");
startActivity(smsIntent);

If you don't want any text then remove the sms_body key.

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:"+shopkepperDataModel.getPhoneNo().trim()));
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(smsIntent);
0

Works up to Android 11. (Supports all supported SDKs)

btnMessageTo.setOnClickListener {
            val phoneNumber = "+8801234567890"
            val intent = Intent("android.intent.action.SENDTO")
            intent.data =
                Uri.parse("smsto:" + Uri.encode(phoneNumber ))
            startActivity(intent)
        }
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86