Jumana
Jumana

Reputation: 83

Share via SMS only

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

Upvotes: 4

Views: 13370

Answers (5)

tanni tanna
tanni tanna

Reputation: 724

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

Upvotes: 0

Daniele Segato
Daniele Segato

Reputation: 12899

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)

Upvotes: 1

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

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!

Upvotes: 19

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

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);        
}

Upvotes: 1

Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

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"));

Upvotes: -2

Related Questions