2

I want to send SMS from my application, for which I have written below code, which is pretty simple. But the issue I am facing is, No activity is started on sending message

Method to send a message:

private void sendSMS(Context context, String phone, String msg){
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);

    smsIntent.setData(Uri.parse("smsto:"));
    smsIntent.putExtra("address", phone);
    smsIntent.putExtra("sms_body", msg);
    smsIntent.setType("vnd.android-dir/mms-sms");

    try {
        startActivity(smsIntent);
        finish();
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
    }
}

Permission added in manifest

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

It always shows toast written in catch()

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Bravado
  • 137
  • 4
  • 10

3 Answers3

2

Try this one:

also there is no need for:

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

Just use this code:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.setData(Uri.parse("sms:"));
                sendIntent.putExtra("sms_body", "Check out My app for your smartphone. Download it today from https://google.com");
                try {
                    startActivity(sendIntent);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();                        
                }
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
  • thanks. your code line sendIntent.setData(Uri.parse("sms:")); helped me out. I was doing "smsto:" form the example i was follwoing and that was breaking the system – John Bravado Jan 09 '17 at 13:30
1

The code you are executing is old. This is the code to send an SMS from your APP

public void sendSMSFunction(){
     SmsManager smsManager = SmsManager.getDefault();
     String messageContent = "Your Content";
     smsManager.sendTextMessage("Destination_PhoneNumber", null, messageContent, null, null);
}

And add this permission to your manifest file

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

And if you are executing this in Android Marshmallow and above you will need to ask Runtime Permission.

Akhil Soman
  • 2,077
  • 2
  • 17
  • 30
  • 1
    I do not want to send an SMS behind someone's back. I merely want to call the default text messenger with the phone number already filled out. But your second response was good – John Bravado Jan 07 '17 at 13:29
  • Sorry i didn't know that. But what you could do is create an activity that contains fields for entering the number, message and two buttons Send and Cancel. And you could send the sms from the button click. This way the user will be in your app the whole time and there will be no sending sms behind anyone's back. – Akhil Soman Jan 09 '17 at 03:31
  • I actually might do that. because it is a relatively simple activity to generate and would look cleaner. i was just trying to prevent the need for permissions. – John Bravado Jan 09 '17 at 13:28
0

As you are getting toast always means its entering into catch(). Catch is handling ActivityNotFoundException

Follow this link, if it helps:

https://stackoverflow.com/a/10613004/5722385

If you are working with marshmallow and above, kindly go through the below link, it is having example how to add Runtime permission

https://stackoverflow.com/a/34969604/5722385

Because Sending sms is grouped into dangerous permission so you need to handle it at Runtime also, only writing permission in menifest file is not enough.

Before adding runtime permission if you want you can enable permission from you phone follow this steps.

Goto settings --> apps --> select you app --> permissions --> enable sms permission.

You can also check this link to get list of Dangerous permissions, that should be handled at Runtime

https://developer.android.com/guide/topics/permissions/requesting.html

Community
  • 1
  • 1
Mrinmoy
  • 1,370
  • 2
  • 18
  • 28