1

How do I send a text message? It seems simple but it doesn't work for me.

I have the permission in the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.healthapp.healthapp">

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

<application   ...

I then have this code in my onClick:

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
                            startActivity(sendIntent);

But when I run this I get "Unfortunately App has stopped."

And the error message of:

FATAL EXCEPTION: main
Process: com.healthapp.healthapp, PID: 13477
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
user2633709
  • 103
  • 2
  • 9

3 Answers3

4

You've got two different methods of sending SMS in your code. If you want to use SmsManager, then you don't need the Intent/startActivity() method, which attempts to open another app to handle the SMS.

You can just remove everything after the smsManager.sendTextMessage() line, and you won't get that Exception anymore.

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Ah thanks, I am using Android Studio and the emulator - is there any way to check that the message has been sent? Maybe by logging something? Or using one of the null parameters of sendTextMessage ?? – user2633709 May 05 '16 at 12:28
  • Well, you're not going to be able to send an actual message to a real device from the emulator. However, you can pass a `PendingIntent` for the fourth argument, which will deliver an `Intent` with a result code to the target component, but I'm not sure how the emulator handles that. That is, I'm not sure if it'll actually deliver a success code if everything's valid, though I would think it should. I've a generic example w/ a `BroadcastReceiver` in my [answer here](http://stackoverflow.com/questions/24673595/how-to-get-sms-sent-confirmation-for-each-contact-person-in-android/24845193#24845193) – Mike M. May 05 '16 at 12:32
  • Thanks, I thought it might be PendingIntent but I'm not sure of the syntax for this. Would I define the PendingIntent above this with: Intent sendIntent = new Intent(Intent.ACTION_VIEW); and then putting sendIntent as a parameter of sendTextMessage ? – user2633709 May 05 '16 at 12:45
  • Using that example, you would copy the `Intent sentIntent = ...` and `PendingIntent sentPI = ...` lines, and pass `sentPI` as the fourth argument in the `sendTextMessage()` call. Lemme see if I can find a more succinct example. That one's for multiple recipients. – Mike M. May 05 '16 at 12:48
  • [This one's](http://stackoverflow.com/questions/5944345/sending-sms-in-android) a little more straightforward. – Mike M. May 05 '16 at 12:53
1

If you want by Intent then

sendSmsByViewIntent()

Intent smsVIntent = new Intent(Intent.ACTION_VIEW);
    // prompts only sms-mms clients
    smsVIntent.setType("vnd.android-dir/mms-sms");

    // extra fields for number and message respectively
    smsVIntent.putExtra("address", phoneNumber.getText().toString());
    smsVIntent.putExtra("sms_body", smsBody.getText().toString());
    try{
        startActivity(smsVIntent);
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

Send by SmsManager then,

sendSmsByManager()

try {
        // Get the default instance of the SmsManager
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber.getText().toString(), 
                null,  
                smsBody.getText().toString(), 
                null, 
                null);
        Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),"Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

send by ,

ACTION_SENDTO

// add the phone number in the data
    Uri uri = Uri.parse("smsto:" + phoneNumber.getText().toString());

    Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
    // add the message at the sms_body extra field
    smsSIntent.putExtra("sms_body", smsBody.getText().toString());
    try{
        startActivity(smsSIntent);
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

should try any one these methods. but you are trying with two types in same time.

Finally main thing is permission at manifest.xml

<uses-permission android:name="android.permission.SEND_SMS"/>
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • Ah thanks, I am using Android Studio and the emulator - is there any way to check that the message has been sent? Maybe by logging something? Or using one of the null parameters of sendTextMessage ?? – user2633709 May 05 '16 at 12:29
0

If "3rd party sms gateway" not working then used this below code.

added permission in manifest file

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

used in activity

 private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;

permission check

protected void sendSMSMessage() {

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {

        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    }else if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            == PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {

        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    }
}

code

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(mobNo, "CP-ICTSBM", "Your otp is "+otp+" . "+"Do not share it with anyone by any means. This is confidential and to be used by you only. Parbhani Citizen ICTSBM", null, null);
                Toast.makeText(getApplicationContext(), "SMS sent.",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                return;
            }
        }
    }

}