4

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

Jumana
  • 83
  • 1
  • 2
  • 9
  • 1
    Why do you want to limit options to only SMS? The strength of sharing intents is to allow the user to choose freely. – Eloff Apr 29 '12 at 09:30

5 Answers5

19

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!

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
1

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);        
}
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
  • This will not show the contacts list already in the device will it? – Jumana Apr 29 '12 at 14:33
  • No, this code snippet will send sms programatically. If you want to use default sms program, use Intents as @sandip armal answered. – Ali Behzadian Nejad Apr 29 '12 at 14:45
  • 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. – Jumana Apr 29 '12 at 14:52
1

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)

Daniele Segato
  • 12,314
  • 6
  • 62
  • 88
0
private void shareViaText(){
        Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
        smsIntent.setData(Uri.parse("smsto:"));
        smsIntent.putExtra("sms_body", "Some text");
        startActivity(smsIntent);
        
    }

If you want to ensure that your intent is handled only by a text messaging app (and not other email or social apps), then use the ACTION_SENDTO action and include the smsto: data scheme

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

tanni tanna
  • 544
  • 4
  • 12
-2

try following code... Add this code in onClickListener() of Button.

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "Here is the share content body";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
  • But this displays all options available. I want only the "Messages" option from the chooser. – Jumana Apr 29 '12 at 14:31
  • This code will display all option to share not for sms solutin provided by Nejab is what you want – Milon Feb 12 '15 at 08:40