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
  • 105
  • 2
  • 9

2 Answers2

3

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.
  • 37,502
  • 8
  • 98
  • 92
  • 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,084
  • 1
  • 17
  • 44
  • 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