0

My app is trying to send a sms to another number. I have the requisite permission(SEND_SMS) granted by the user. The message however does not get sent.The default messaging app display the message with a "failure to send/Resend" prompt. What is the reason behind this and how do I fix this? My code is pretty straightforward-

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneno, null, txt, null, null);

Note- My app operates well below the sms frequency limit Edit : It works fine when run from an activity but fails when run from a service. What's the reasoning behind this ?

anirudh
  • 424
  • 1
  • 4
  • 12

2 Answers2

0

The problem seemed to be because of where the code was getting called from . I had a service (running on a seperate thread) spawn a new thread to send out the SMSs , for some reason this seemed to lead to problems. After I removed all that unnecessary complexity and have the sms sent directly from the service it works fine.

anirudh
  • 424
  • 1
  • 4
  • 12
-1

Use this code instead:

Intent sInt = new Intent(Intent.ACTION_VIEW);
sInt.putExtra("address", new String[]{txtMobile.getText().toString()});
sInt.putExtra("sms_body",txtMessage.getText().toString());
sInt.setType("vnd.android-dir/mms-sms");

Make sure you have this tag in your manifest:

<uses-permission android:name="android.permission.SEND_SMS"/>
Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71
  • There is no certainty that anything vnd.android exists on the device. Plus this would bring up an app for editing an SMS, which may not be what he wants – Gabe Sechan Jun 03 '19 at 18:40
  • I need a fully automated solution that is device independent which is why I am using what I am now. – anirudh Jun 03 '19 at 18:41