Share via SMS only

12,552

Solution 1

To send sms using intents, use this code:

String smsBody="Sms Body";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", smsBody); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

I hope this help!

Solution 2

Steps to enable sending sms:

1- In android-manifest, add sending sms permission as below:

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

2- In yout Activity add this method:

public void sendSms(final String receiverNumber, final String smsBody) {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(receiverNumber, null, smsBody, null, null);        
}

Solution 3

This is the only correct way to send SMS/MMS via intent:

https://developer.android.com/guide/components/intents-common#SendMessage

No setType("vnd.android-dir/mms-sms") or other shenanigans are needed.

There's an official documentation for intents like this and should be followed.

For an SMS all you need is this:

fun composeSmsMessage(message: String) {
    val intent = Intent(Intent.ACTION_SENDTO).apply {
        type = HTTP.PLAIN_TEXT_TYPE
        data = Uri.parse("smsto:")  // This ensures only SMS apps respond
        putExtra("sms_body", message)
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
}

If you have the recipient for the SMS/MMS you can add it in the data Uri after smsto:, ex smsto:+123456789), otherwise the user will pick the recipient in the SMS/MMS app.

If the text is too long it will be sent as MMS, if you need to send an MMS with an image or video you do it similarly but add an Uri as Intent.EXTRA_STREAM and specify the type accordingly (image/video). The Uri needs to be handled by an exposed content provider providing the correspondent image or video.

on a personal note...

(I wish there was a way in stack overflow to vote for "wrong answer" to avoid people just copying the wrong thing into their app)

Share:
12,552
Jumana
Author by

Jumana

Currently and Android programming enthusiast

Updated on June 04, 2022

Comments

  • Jumana
    Jumana almost 2 years

    My app has an image button which on click should send a piece of text via SMS only. How do i get my app to do that? Please help.

    I also want to the user to be able to choose a contact from the list of contacts on his device.

    Jumana

    • Eloff
      Eloff about 12 years
      Why do you want to limit options to only SMS? The strength of sharing intents is to allow the user to choose freely.
  • Jumana
    Jumana about 12 years
    But this displays all options available. I want only the "Messages" option from the chooser.
  • Jumana
    Jumana about 12 years
    This will not show the contacts list already in the device will it?
  • Ali Behzadian Nejad
    Ali Behzadian Nejad about 12 years
    No, this code snippet will send sms programatically. If you want to use default sms program, use Intents as @sandip armal answered.
  • Jumana
    Jumana about 12 years
    thanks @Ali Behzadian Nejad. But i want the user to be given the option of only sharing through SMS. The intent chooser displays twitter, facebook etc right? And the user should also be able to pick a contact from his contact list to send the SMS to.
  • Kirill Kulakov
    Kirill Kulakov over 10 years
    No Activity found to handle Intent on kitkat
  • Ali Behzadian Nejad
    Ali Behzadian Nejad over 10 years
    It seems that KitKat has changed SMS API. I did not try new API.
  • Kirill Kulakov
    Kirill Kulakov over 10 years
  • cnfw
    cnfw over 9 years
    What's the String name for the SMS "To" field? Rather than filling text into the body, I'd like to fill a number into the "To" field. Thanks EDIT: It's fine, I got it, it is "address"
  • Milon
    Milon about 9 years
    This code will display all option to share not for sms solutin provided by Nejab is what you want